Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Plus One LeetCode Java

// https://leetcode.com/problems/plus-one/

class Solution {
    public int[] plusOne(int[] digits) {
        for (int i = digits.length -1 ; i >= 0; i-- ){
            if (digits[i] < 9){
                digits[i]++;
                return digits;
            }
            digits[i] = 0;
        }
        
        int[] result = new int[digits.length + 1];
        result[0] = 1;
        return result;
    }
}
 
PREVIOUS NEXT
Tagged: #Plus #One #LeetCode #Java
ADD COMMENT
Topic
Name
4+5 =