您的位置:首页 > 其它

66. Plus One (计算数字加1后的结果)

2016-10-27 22:00 141 查看
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 Solution {
public int[] plusOne(int[] digits) {
int i = digits.length - 1;
while (i >= 0) {
if (digits[i] == 9) {
digits[i] = 0;
i--;
} else {
digits[i]++;
break;
}
}if(i<0){
int[] res = new int[digits.length+1];
res[0]=1;
for(int j=0;j<digits.length;j++){
res[j+1]=digits[j];
return res;
}
}return digits;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法
相关文章推荐