题目链接
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):

这篇博客主要讲解LeetCode中的第66题,即如何对一个用数组表示的非负数进行加一操作。内容包括题目描述、解题思路以及两种不同的解决方案,涉及从后向前扫描数组并处理进位的方法。适合Python编程爱好者和LeetCode初学者阅读。
最低0.47元/天 解锁文章
1363

被折叠的 条评论
为什么被折叠?



