Plus One

Question

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.

_*Example_8

Given [1,2,3] which represents 123, return [1,2,4].

Given [9,9,9] which represents 999, return [1,0,0,0].

Tags

Array Google

Related Problems

Medium Divide Two Integers 15 % Easy Add Binary

Analysis

比较巧的方法是将 +1 当做末位digit的进位carries;每次进位 carries = sum / 10;,留下的数字为 digits[i] = sum % 10;

如果最终进位 != 0,说明要新增加一位,则新建数组把计算结果填进去。

Solution

好久不见,写了一个特啰嗦的...:(0 ms beats 100% AC)

Last updated

Was this helpful?