LeetCode66. Add One

Given a non -empty array of non -negative integers , add one to the number and return a new array.

The highest digit is stored in the first position of the array, and each element in the array stores only one number.

You can assume that this integer will not start with zero except for the integer 0.

Example 1:

Input: [1,2,3]
 Output: [1,2,4]
 Explanation: The input array represents the number 123.

Example 2:

Input: [4,3,2,1]
 Output: [4,3,2,2]
 Explanation: The input array represents the number 4321.

class Solution {
    public int[] plusOne(int[] dights) {
        if(dights==null||dights.length==0) {
    		return null;
    	}
    	 int i;
    	 int len=dights.length;
    	 dights [len-1] + = 1;
    	 for(i=len-1;i>0;i--) {
    		 if(dights[i]>=10) {
    			 dights[i]%=10;
    			 dights[i-1]+=1;
    		 }else {
    			 break;
    		 }
    	 }
    	 if(dights[0]==10) {
    		 int[] a=new int[len+1];
    		 a[0]=1;
    		 dights[0]=0;
    		 for(i=0;i<len;i++) {
    			 a[i+1]=dights[i];
    		 }
    		 return a;
    	 }
    	return dights;
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324906972&siteId=291194637