Sunday, March 8, 2015

66. Plus One Leetcode Java

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.
Solution:
The integer was stored in an array. A number plus one may have +1 length but only when the number is 9 or 99....
And also the carry will only happen when there are trailing '9'. 
So check from the end of the array to start until the digit is not '9', 
if all digits are '9', initiate a new array with length+1, and initiate it with res[0]=1;
else, initiate a new array with original length, and set the first non '9' with res[ind]=num[i]+1, set res[ind+1] to res[end] to 0 (by default they are already 0) and copy all the numbers from 0 to ind-1 from num to res.
Time complexity: O(n)
 public int[] plusOne(int[] digits) {  
   if(digits==null || digits.length==0) return digits;  
   int i=digits.length-1;  
   while(i>=0 && digits[i]==9) i--;  
   if(i==-1){  
     int[] res=new int[digits.length+1];  
     res[0]=1;  
     return res;  
   }  
   int[] res=new int[digits.length];  
   res[i]=digits[i]+1;  
   for(int j=i-1;j>=0;j--){  
     res[j]=digits[j];  
   }  
   return res;  
   }  

No comments:

Post a Comment