Add one to a big-endian digit array by scanning from the right. Args: digits: Non-empty list of digits representing a non-negative integer. Returns: List of digits representing the input number plus one. Examples: >>> plus_one_v2([1, 2, 9]) [1, 3, 0]
(digits: list[int])
| 44 | |
| 45 | |
| 46 | def plus_one_v2(digits: list[int]) -> list[int]: |
| 47 | """Add one to a big-endian digit array by scanning from the right. |
| 48 | |
| 49 | Args: |
| 50 | digits: Non-empty list of digits representing a non-negative integer. |
| 51 | |
| 52 | Returns: |
| 53 | List of digits representing the input number plus one. |
| 54 | |
| 55 | Examples: |
| 56 | >>> plus_one_v2([1, 2, 9]) |
| 57 | [1, 3, 0] |
| 58 | """ |
| 59 | length = len(digits) |
| 60 | for index in range(length - 1, -1, -1): |
| 61 | if digits[index] < 9: |
| 62 | digits[index] += 1 |
| 63 | return digits |
| 64 | digits[index] = 0 |
| 65 | digits.insert(0, 1) |
| 66 | return digits |
| 67 | |
| 68 | |
| 69 | def plus_one_v3(num_arr: list[int]) -> list[int]: |