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.
将数组看出一个大数,对大数加1,注意进位如果最高位要进位,则需开辟新数组
public class Solution {
public int[] plusOne(int[] digits) {
int len=digits.length;
if(len==0)
return digits;
int tmp=1,num;
for(int i=len-1;i>=0;i--){
num=digits[i];
digits[i]=(num+tmp)%10;
tmp=(num+tmp)/10;
}
int[] res=null;
if(tmp==1){ //最高位需要进位
res=new int[len+1];
res[0]=tmp;
<span style="background-color: rgb(255, 255, 153);">System.arraycopy(digits, 0, res, 1, len); </span> //数组拷贝
return res;
}
else return digits;
}
}