plus_one.py 411 B

12345678910111213141516171819
  1. from typing import List
  2. def plusOne(digits: List[int]) -> List[int]:
  3. i = len(digits) - 1
  4. digits[i] = digits[i] + 1
  5. while i > 0:
  6. if digits[i] == 10:
  7. digits[i - 1] = digits[i - 1] + 1
  8. digits[i] = 0
  9. i -= 1
  10. else:
  11. break
  12. if digits[0] == 10:
  13. digits[0] = 0
  14. digits.insert(0, 1)
  15. return digits
  16. print(plusOne([9,9,9,0]))