Problem
Problem page: https://leetcode.com/problems/plus-one/
Topics: Array, Math
Difficulty: Easy
Similar problems: (M) Multiply Strings, (E) Add Binary
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Analysis
An int array represents a number, with the most significant digit on the left. Add 1 and return the result as an int array.
Just simulate addition and watch the carry. For numbers like 99 or 999, you also need to insert a 1 at the front of the array after adding 1.
The space complexity is O(1) whether or not there's a carry out of the most significant digit. The time complexity is O(N).
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-plus-one/
