1ms Java Simple solution


  • 0
    public class Solution {
        public int[] plusOne(int[] digits) {
            int n=digits.length;
            int i=n-1; int carry=1;
            while(i>=0){
                int temp=digits[i]+carry;
                digits[i]=(digits[i]+carry)%10;
                if(temp<10){
                    carry=0;
                }
                i--;
            }
            if(carry==0){
                return digits;
            }
            int results[] = new int[n+1];
            results[0]=1;
            for(int k=1; k<results.length;k++){
                results[k]=digits[k-1];
            }
            return results;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.