leetcode 66 plus one

xwang24x (2019-12-05 01:39:25) 评论 (0)

Knowledge base:

1. array from back 

2      <9 add and return

3  =9 change the value to 0 and next loop plus one

4 all value are 0 create a new array

public int[] plusOne(int[] digits) {
        if(digits==null||digits.length==0) return digits;
        int index=digits.length-1;
        
        while(index>=0){
            if(digits[index]<9){
                digits[index]+=1;
                return digits;
            }
            else
                digits[index]=0;
            index--;
                
        }
        int [] res=new int[digits.length+1];
        res[0]=1;
        return res;
        
    }