#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Plus One
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.
'''
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
length = len(digits)
if length == 0:
return [1]
index = length - 1
sum = 1
while index >= 0 and sum > 0:
sum += digits[index]
digits[index] = sum%10
sum /= 10
index -= 1
if sum > 0:
digits.insert(0,sum)
return digits
if __name__ == "__main__":
s = Solution()
print s.plusOne([9,9])
print s.plusOne([1,2])
print s.plusOne([1,9])
58 leetcode - Plus One
最新推荐文章于 2024-07-22 16:27:42 发布