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 { 2 public int[] plusOne(int[] digits) { 3 4 int i=digits.length-1; 5 int c=0; 6 digits[i]=digits[i]+1; 7 8 try{ 9 while(digits[i]>=10&&i>=0) 10 { 11 digits[i]=digits[i]-10; 12 c=1; 13 i--; 14 if(i>=0) 15 { 16 digits[i]=digits[i]+c; 17 c=0; 18 } 19 } 20 }catch(ArrayIndexOutOfBoundsException e){} 21 22 if(c==1) 23 { 24 int[] num= new int[digits.length+1]; 25 num[0]=1; 26 for(int j=0;j<digits.length;j++) 27 num[j+1]=digits[j]; 28 return num; 29 } 30 31 return digits; 32 } 33 }
浙公网安备 33010602011771号