题目链接
https://leetcode.com/problems/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.
题目翻译
给定一个用数组表示的非负数,对这个数加一。这个数组里的数字最高位在列表头部。
思路方法
这个题目有点难理解,应该举个例子的。。。大概就是,比如:
数字1234用数组表示是[1,2,3,4],加一后数组表示为[1,2,3,5];数字9999用数组表示是[9,9,9,9],加一得到[1,0,0,0,0]。
思路一
从后向前扫描数组,每位加一,有进位则变成0且保留进位。最高位如果是9且有进位则要在数组前面多加一个元素1。
代码
class Solution(object)