66. Plus One

You are given a large integer represented as an integer array digits, where each digitsi is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

Example 1:

Input: digits = 1,2,3

Output: 1,2,4

Explanation: The array represents the integer 123.

Incrementing by one gives 123 + 1 = 124.

Thus, the result should be 1,2,4.

Example 2:

Input: digits = 4,3,2,1

Output: 4,3,2,2

Explanation: The array represents the integer 4321.

Incrementing by one gives 4321 + 1 = 4322.

Thus, the result should be 4,3,2,2.

Example 3:

Input: digits = 9

Output: 1,0

Explanation: The array represents the integer 9.

Incrementing by one gives 9 + 1 = 10.

Thus, the result should be 1,0.

Constraints:

复制代码
1 <= digits.length <= 100
0 <= digits[i] <= 9
digits does not contain any leading 0's.

链接: [66. Plus One

](https://leetcode.cn/problems/plus-one/description/

思路一:

这道题可以先求数组中的几个数构成的数字(使用::-1,加一,之后再成为数组。

python 复制代码
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        #return [int(i) for i in str(int(''.join([str(j) for j in digits]))+1)]
        a=[i*10**index for index,i in enumerate(digits[::-1])]
        num=sum(a)+1
        return [int(x) for  x in str(num)]

a='python'

b=a::-1

print(b) #nohtyp

c=a::-2

print© #nhy

#从后往前数的话,最后一个位置为-1

d=a:-1 #从位置0到位置-1之前的数

print(d) #pytho

e=a:-2 #从位置0到位置-2之前的数

print(e) #pyth

b = ai:j # 表示复制ai到aj-1,以生成新的list对象

a = 0,1,2,3,4,5,6,7,8,9

b = a1:3 # 1,2

#当i缺省时,默认为0,即 a:3相当于 a0:3

#当j缺省时,默认为len(alist), 即a1:相当于a1:10

#当i,j都缺省时,a:就相当于完整复制一份a

b = ai:j:s # 表示:i,j与上面的一样,但s表示步进,缺省为1.

#所以ai:j:1相当于ai:j

#当s<0时,i缺省时,默认为-1. j缺省时,默认为-len(a)-1

#所以a::-1相当于 a-1:-len(a)-1:-1,也就是从最后一个元素到第一个元素复制一遍,即倒序。

思路二:

这道题可以先求数组中的几个数构成的数字(使用str,加一,之后再成为数组。

python 复制代码
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        return [int(i) for i in str(int(''.join([str(j) for j in digits]))+1)]

思路三:for循环遍历

python 复制代码
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        num = 0
        for n in range(len(digits)):
            num += (digits[len(digits)-n-1] * 10 ** n)
        return [int(n) for n in str(num+1)]
相关推荐
程序员杰哥7 小时前
接口自动化测试:多环境配置实战
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
zh路西法7 小时前
【OpenCV无人机光流速度估计】基于Farneback稠密光流方法的无人机速度估计
人工智能·python·opencv·计算机视觉·无人机
罗超驿7 小时前
11.LeetCode 1004. 最大连续1的个数 III | 滑动窗口解法详解(Java)
java·算法·leetcode
聆风吟º7 小时前
【Python编程日志】Python基础语法:常量 | 表达式 | 变量
开发语言·python·变量·常量·表达式
05候补工程师7 小时前
【英语学习笔记】基于“底层逻辑转换”与“去动词化”的英汉互译核心方法论及写作高分公式
经验分享·笔记·学习·考研
weixin_468466857 小时前
Airtable 零基础快速上手与实战指南
数据库·人工智能·python·深度学习·ai·大模型
大明者省8 小时前
CentOS 与 Ubuntu Python 部署差异
笔记·python·ubuntu·centos
容沁风8 小时前
本地用pptx和大模型生产PPT课件
python·大模型·pptx
bbaydnog8 小时前
FreeRTOS学习笔记 17:资源管理与临界区保护——优先级反转、死锁,90%的RTOS bug都跟它有关
笔记·学习·bug
codefan※8 小时前
pytorch安装流程
人工智能·pytorch·python