【Video】Iterate through the array from the end
Intuition\nIterate through the array from the end\n\n---\n\n# Solution Video\n\nhttps://youtu.be/9Ea9RH7Dm-g\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to su
# Intuition\nIterate through the array from the end\n\n---\n\n# Solution Video\n\nhttps://youtu.be/9Ea9RH7Dm-g\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nSubscribers: 6,990\nThank you for your support!\n\n---\n\n# Approach\n\nLet\'s think about this.\n\n```\nInput: digits = [1,2,3]\n```\nThe description says "Increment the large integer by one and return the resulting array of digits", so we should add `1` to the last index then return the array.\n\n```\n[1,2,4]\n```\nBut what if we have \n```\nInput: digits = [9,9]\n```\nIf we add `1` to the last index, the number at the last index will be `0` then we have `carry`. Again if we add the carry to the index `0`, the number at index `0` will be `0`, then we will get `another carry`.\n\nFinally, add `1` and `1` will be the first index and return the array.\n\n```\nreturn [1,0,0]\n``` \n\n\n---\n\n\u2B50\uFE0F Points\n\nWhen we add `1` then the number is not `10`, just add `1` to the last index and return the array.\n\nIf `10`, add `0` to the current index, add `0` to the current index then continue to loop until `we find a number except 10` or `reach index 0`.\n\nIf we reach index `0`, then add `1` to the first index and other all numbers should be `0`, **because we have `10` in each digit and have `carry` for the next digit.**\n\n---\n\nhttps://youtu.be/bU_dXCOWHls\n\n---\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n```python []\nclass Solution:\n    def plusOne(self, digits: List[int]) -> List[int]:\n\n        for i in range(len(digits) - 1, -1, -1):\n\n            if digits[i] + 1 != 10:\n                digits[i] += 1\n                return digits\n            \n            digits[i] = 0\n\n            if i == 0:\n                return [1] + digits\n```\n```javascript []\nvar plusOne = function(digits) {\n    for (let i = digits.length - 1; i >= 0; i--) {\n        if (digits[i] + 1 !== 10) {\n            digits[i] += 1;\n            return digits;\n        }\n        digits[i] = 0;\n        if (i === 0) {\n            digits.unshift(1);\n            return digits;\n        }\n    }    \n};\n```\n```java []\nclass Solution {\n    public int[] plusOne(int[] digits) {\n        for (int i = digits.length - 1; i >= 0; i--) {\n            if (digits[i] + 1 != 10) {\n                digits[i] += 1;\n                return digits;\n            }\n            digits[i] = 0;\n        }\n        \n        int[] newDigits = new int[digits.length + 1];\n        newDigits[0] = 1;\n        return newDigits;        \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<int> plusOne(vector<int>& digits) {\n        for (int i = digits.size() - 1; i >= 0; i--) {\n            if (digits[i] + 1 != 10) {\n                digits[i] += 1;\n                return digits;\n            }\n            digits[i] = 0;\n            if (i == 0) {\n                digits.insert(digits.begin(), 1);\n                return digits;\n            }\n        }\n        return digits;        \n    }\n};\n```\n\n---\n\nThank you for reading my post. Please upvote it and don\'t forget to subscribe to my channel!\n\n### \u2B50\uFE0F Subscribe URL\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\n### \u2B50\uFE0F Twitter\nhttps://twitter.com/CodingNinjaAZ\n\n### \u2B50\uFE0F #189 - Rotate Array\n\nhttps://youtu.be/fDRPdN0dlcs\n\n
['C++', 'Java', 'Python3', 'JavaScript']