Plus One [Easy]
2014-12-17 02:17
281 查看
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.
===============================Answer===============================================
public class Solution {
public int[] plusOne(int[] digits) {
if(digits==null||digits.length==0) return null;
for(int i=digits.length-1;i>=0;i--) {
if(digits[i]==9) digits[i]=0;
else {digits[i]=digits[i]+1; return digits;}
}
int[] returnList = new int[digits.length+1];
returnList[0]=1;
for(int i=1;i<returnList.length;i++) {
returnList[i]=0;
}
return returnList;
}
}
The digits are stored such that the most significant digit is at the head of the list.
===============================Answer===============================================
public class Solution {
public int[] plusOne(int[] digits) {
if(digits==null||digits.length==0) return null;
for(int i=digits.length-1;i>=0;i--) {
if(digits[i]==9) digits[i]=0;
else {digits[i]=digits[i]+1; return digits;}
}
int[] returnList = new int[digits.length+1];
returnList[0]=1;
for(int i=1;i<returnList.length;i++) {
returnList[i]=0;
}
return returnList;
}
}
相关文章推荐
- LeetCode66. Plus One-python(easy)
- 66. Plus One [easy] (Python)
- 66. Plus One 难度:easy
- Leetcode 66. Plus One (Easy) (cpp)
- [Leetcode 66, Easy] Plus One
- 【leetcode】Plus One (easy)
- LeetCode】66.Plus One(easy)解题报告
- 【Leetcode】【Easy】Plus One
- LeetCode-66-Plus One(水题/数值计算)-Easy
- Easy 66题 Plus One
- 【LeetCode】(66)Plus One (Easy)
- leetcode 066 Plus One(难易度:Easy)
- Easy 14 Plus One(66)
- Leet Code OJ 66. Plus One [Difficulty: Easy]
- 5.[easy]Plus one
- Easy-题目30:66. Plus One
- centOS6.5 python升级和安装easy_install,pip
- Codeforces 913D - Too Easy Problems(贪心+优先队列)
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)