0ms Java solution


  • 0
    C
    public class Solution {
        public int[] plusOne(int[] digits) {
            int carry = 0;
            for(int i=digits.length-1;i>=0;i--){
                if(i==digits.length-1){
                    digits[i]+=1;
                }
                if(digits[i]+carry >9){
                    digits[i] = 0;
                    carry = 1;
                }
                else{
                    digits[i]+=carry;
                    carry = 0;
                }
            }
            if(carry == 1){
                int [] result = new int[digits.length+1];
                result[0] = carry;
                return result;
            }
            return digits;
        }
    }

Log in to reply
 

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