题目:
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
真的,只有能读懂题才能找着工作,我一开始以为是每个字符加一,MMP!But,你说这个东西有个啥实际意义??
给定一个数组,这个数组代表一个数字,数字的最高位存在数组最前面,数字的最低位存在数组的最后面(符合书写习惯),输出该数字加1得到的数组例如 input:[9,9] output:[1,0,0]
solution:
1、数学方法,*10+digits[i]
class Solution: def plusOne(self, digits): num = 0 for i in range(len(digits)): num = digits[i] + num * 10 print(num) return [int(i) for i in str(num+1)]
2、join(),把原来字符串的形式转为数字,+1,再转回字符串的形式
def plusOne(self, digits): if len(digits)==0: return False number=int(''.join([str(i) for i in digits]))+1 nList=[int(i) for i in str(number)] return nList
但是,第一种方法时间比较短哦~