心得:想清楚,打草稿,避免不必要的错误
记录是否进位
class Solution: # @param {integer[]} digits # @return {integer[]} def plusOne(self, digits): i, carry = -1, True while(carry): if digits[i]==9: digits[i] = 0 if i==-len(digits): digits = [1] + digits carry = False i -= 1 else: digits[i] += 1 carry = False return digits
是九变0,最高位加[1]
class Solution: # @param {integer[]} digits # @return {integer[]} def plusOne(self, digits): i = -1 while(not i<-len(digits)): if digits[i] != 9: digits[i] += 1 return digits else: digits[i] = 0 i -= 1 digits = [1] + digits return digits