Leetcode: Plus one23 Sep 2022 by Friedrich Ewald · 2 min read
A decimal number is given as a list of integers. The number should be incremented by
1. The idea here is to iterate backwards through the list and start with the smallest digit. This way, this challenge can be solved in
O(n). If after adding
0 it means we had an overflow and need to add one. If we didn’t we’re done and can break the loop. As a final check we need to make sure that there is no overflow in the end which would cause us to prepend the list.
Runtime: 67 ms, faster than 12.59% of Python3 online submissions for Plus One.
Memory Usage: 13.9 MB, less than 59.15% of Python3 online submissions for Plus One.