Accepted 176ms Java Solution


  • 0
    Z
        public class Solution {
        public int[] plusOne(int[] digits) {
            for(int i = digits.length - 1; i >=0; i--){
                int digit = digits[i] + 1;
                if(digit < 10){
                    digits[i] = digit;
                    break;
                } else {
                    digits[i] = digit % 10;
                } 
                if(i == 0){
                    int[] oldDigits = digits;
                    digits = new int[digits.length + 1];
                    digits[0] = 1;
                    System.arraycopy(oldDigits, 0, digits, 1, oldDigits.length);
                }
            }
            return digits;
        }
    }

Log in to reply
 

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