leetcode — plus-one
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/plus-one/
*
*
* Given a non-negative number represented as an array of digits, plus one to the number.
*
* The digits are stored such that the most significant digit is at the head of the list.
*
*/
public class PlusOne {
/**
* 加法运算,注意进位
*
* @param digit
* @return
*/
public Integer[] plusOne (int[] digit) {
int carry = 1;
List<Integer> result = new ArrayList<Integer>();
for (int i = digit.length - 1; i > -1; i--) {
carry += digit[i];
result.add(0, carry % 10);
carry = carry / 10;
}
if (carry > 0) {
result.add(0, carry);
}
return result.toArray(new Integer[result.size()]);
}
public static void main(String[] args) {
PlusOne plusOne = new PlusOne();
int[] arr = new int[]{1,2,3};
int[] arr1 = new int[]{1,9,9};
int[] arr2 = new int[]{9,9,9};
int[] arr3 = new int[]{1};
int[] arr4 = new int[]{9};
int[] arr5 = new int[]{};
System.out.println(Arrays.toString(plusOne.plusOne(arr)));
System.out.println(Arrays.toString(plusOne.plusOne(arr1)));
System.out.println(Arrays.toString(plusOne.plusOne(arr2)));
System.out.println(Arrays.toString(plusOne.plusOne(arr3)));
System.out.println(Arrays.toString(plusOne.plusOne(arr4)));
System.out.println(Arrays.toString(plusOne.plusOne(arr5)));
}
}
leetcode — plus-one的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- centos7 启动tomcat卡盾
vim $JAVA_HOME/jre/lib/security/java.security securerandom.source=file:/dev/random 改为 securerandom.s ...
- "hello,world"———C++入门有感
刚进入这所学校时,编程对于我来说应该算得上一个既熟悉又陌生的词语.虽然曾经耳边不断有人不断提到编程语言,C语言,程序猿等词语,但是作为一个外行人在来到这所学校之前,对于其中的奥秘还是没什么特别了解,仅 ...
- gson的特殊用法
1.gson包在处理 字符串转 Map 或者 List 的方法. List memberList = gson.fromJson(str,new TypeToken<List>() {}. ...
- MyBatis generator配置 overwrite 文件覆盖失效
工具:IDEA.jdk1.8.mysql 底部有解决方法! pom.xml配置 <plugins> <!--Mybatis自动代码插入--> <plugin> &l ...
- python random库
random模块 >>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.766433866365 ...
- 【洛谷P2584】【ZJOI2006】GameZ游戏排名系统题解
[洛谷P2584][ZJOI2006]GameZ游戏排名系统题解 题目链接 题意: GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在 ...
- kubernetes CRD学习笔记
前言 最近在极客时间订阅了kubernetes的专栏,这篇文章是想记录一下自己学习CRD(custom resource definition)的过程,加深一下记忆. 准备工作 首先安装一下我们用的g ...
- noip第32课资料
- ACM计划
原文 :http://027xbc.blog.163.com/blog/static/128159658201141371343475/ ACM主要是考算法的,主要时间是花在思考算法上,不是花在写程序 ...
- linux 解决乱码问题
乱码分两种情况: 1.终端(纯 shell 界面)的乱码 vi /etc/profile export LC_ALL="zh_CN.GB18030:zh_CN.GB2312:zh_CN.G ...