1 解题思想
这道题,就是做一个加法,加1的操作而已。。
要点:
临时数组需要开一个比输入长1的,防止最高位进位。
低位加一,然后处理进位,如有进位,那么高一位执行+1操作,如此往复迭代。
2 原题
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.
3 AC解
public class Solution {
//直接相加就好
public int[] plusOne(int[] digits) {
int c=0;
for(int i=digits.length-1;i>=0;i--){
digits[i]++;
c=digits[i]/10;
if(c==0)
break;
digits[i]%=10;
}
if(c==0)
return digits;
int[] newdigits=new int[digits.length+1];
newdigits[0]=1;
for(int i=0;i<digits.length;i++)
newdigits[i+1]=digits[i];
return newdigits;
}
}