Java || Runtime 0 ms || Beats 100% ||
Intuition\nMy initial approach to solving this problem involves adding one to the given integer represented by an array of digits. I\'ll iterate through the arr
# Intuition\nMy initial approach to solving this problem involves adding one to the given integer represented by an array of digits. I\'ll iterate through the array of digits from right to left, simulating the process of adding one to the number.\n\n# Approach\nI will iterate through the array of digits from right to left. For each digit, I\'ll check if it\'s 9. If it\'s 9, I\'ll set it to 0 and continue iterating. If it\'s not 9, I\'ll increment it by one and immediately return the modified array. This is because there\'s no need to carry over to the next digit in this case.\n\nIf the loop completes without returning, it means all digits were 9, and we need to add a new leading digit. I\'ll create a new array with a length one more than the original array, set the first element to 1, and return the new array.\n\n# Complexity\n- Time complexity: O(n)\n  The algorithm iterates through the array of digits once, where \'n\' is the length of the array.\n\n- Space complexity: O(n) or O(n+1)\n  In the worst case, the algorithm creates a new array with a length one more than the original array. Thus, the space complexity can be considered O(n+1).\n\n# Code\n```\nclass Solution {\n    public int[] plusOne(int[] digits) {\n        for (int i = digits.length - 1; i >= 0; i--) {\n            if (digits[i] == 9) {\n                digits[i] = 0;\n            } else {\n                digits[i]++;\n                return digits;\n            }\n        }\n        \n        int[] newDigits = new int[digits.length + 1];\n        newDigits[0] = 1;\n        return newDigits;\n    }\n        \n\n    }\n\n\n\n```\n![c0504eaf-5fb8-4a1d-a769-833262d1b86e_1674433591.3836212.webp](https://assets.leetcode.com/users/images/8a3b41d1-f0ca-472f-a9f7-8346b34a824e_1693297402.2918687.webp)
['Array', 'Math', 'Java']