python LeetCode 刷题记录 66

题目

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

bash 复制代码
示例
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。

代码

bash 复制代码
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        a = str(int("".join(str(digit) for digit in digits)) + 1)
        out = [int(digit) for digit in a]
        return out
bash 复制代码
.join() 是一个字符串方法,用于将一个可迭代对象(通常是列表)中的元素连接成一个字符串,可以指定一个连接符作为参数。

fruits = ["苹果", "香蕉", "橙子", "葡萄"]
# 使用逗号作为连接符连接列表中的元素
result = ", ".join(fruits)
print(result)
输出:苹果, 香蕉, 橙子, 葡萄

# 使用空字符串作为连接符连接列表中的元素
result = "".join(fruits)
print(result)
输出:苹果香蕉橙子葡萄
bash 复制代码
s = '123'
ls = [a for a in s] # ls:['1', '2', '3']
ls = [int(a) for a in s] # ls:[1, 2, 3]
相关推荐
IVEN_17 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang18 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮19 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling19 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮1 天前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽1 天前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健2 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞2 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽2 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers