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]
相关推荐
kobe_OKOK_19 分钟前
DRF接口幂等操作
python·django
廿士1 小时前
python脚本使用相关
python
青 春 记 忆1 小时前
零基础入门python19:Flask账本第一步——应用工厂、蓝图和健康检查
python·flask·后端开发
朦胧之1 小时前
Python 后端核心知识
python
denggun123452 小时前
yield
前端·数据库·python
zx_741484813 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
招财小梗3 小时前
沈阳商贸公司AI企业服务优势几何?
大数据·人工智能·python
ZISHU_9874 小时前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉
m0_617493945 小时前
SSLError [ASN1: NOT_ENOUGH_DATA] 问题排查与解决指南
python·ssl
whcyhhh5 小时前
头歌实践教学平台:数据科学与大数据技术导论(七上)
大数据·数据库·python