您的位置:首页 > 其它

66. Plus One

2016-01-15 11:37 211 查看
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) {
if(digits == null || digits.length == 0) return new int[]{1};
int l = digits.length;
int  add = (digits[l-1] + 1)/10;
digits[l-1] = (digits[l-1] + 1)%10;
int i = l-2;
for( ; i >= 0 && add > 0;i--){
int tmp = digits[i] + add;
add = tmp/10;
digits[i] = tmp%10;
}
if(add == 0) return digits;
int[] r = new int[l+1];
r[0] = 1;
for(i=1 ; i <= l;i++){
r[i] = digits[i-1];
}
return r;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: