题目:
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.
思路:
貌似没有什么特别的方法,直接使用正常的思路。O(n)
Code:
public int[] plusOne(int[] digits) {
if(digits.length==0) return digits;
int carry=0;
digits[digits.length-1]++;
for(int i=digits.length-1;i>=0;i--){
digits[i]+=carry;
carry=digits[i]/10;
digits[i]%=10;
if(i==0 && carry==0) return digits;
}
digits=new int[digits.length+1];
digits[0]=1;
return digits;
}
备注: