Given a non-negative number represented as an array of digits, plus one to the number.
The digits is stored such, the most significant digit was at the head of the list.
Subscribe to see which companies asked this question
Problem Solving Analysis:
First see the topic, did not understand the reason, here to explain how to understand test instructions.
Think of a list as a number, say [9, 9]
This is considered to be 99, then add one, or 100, which is list, [1, 0, 0]
The way to do this here is to start with the last one and then make a judgment on each array to see if it needs rounding.
A for if there is a carry, add one, no carry words plus 0.
#-*-Coding:utf-8-*-__author__ = ' Jiuzhang ' class solution (object): def plusone (self, digits): plus = 1 for I in xrange (len (digits)-1,-1,-1): digits[i] + = plus if digits[i] >=: digits[i]-= ten plus = 1
else: plus = 0 break if i = = 0 and plus = = 1: digits.insert (0, 1) return digits
(Leetcode) Plus one---plus a