Java Solution


  • 0

    '''

    class Solution {
    public int[] plusOne(int[] digits) {

        boolean increment = true;
        
        for (int i = digits.length-1; i >= 0; i--) {
            
            if (increment) {
                digits[i] = (digits[i] + 1) % 10;
                
                if (digits[i] != 0) {
                    increment = false;
                }
            } else {
                break;
            }
        }
        
        if (increment) {
            int[] arr = new int[digits.length + 1];
            arr[0] = 1;
            
            for (int i = 1; i < arr.length; i++) {
                arr[i] = digits[i-1];
            }
            
            return arr;
        } else {
            return digits;
        }
    }
    

    }

    '''


Log in to reply
 

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