python LeetCode brush test record 66

topic

Given a nonnegative integer represented by a nonempty array of integers, add one to the number.

The highest digit is stored at the beginning of the array, and each element in the array stores only a single number.

You can assume that other than the integer 0, the integer will not start with zero.

示例
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321

code

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
.join() 是一个字符串方法,用于将一个可迭代对象(通常是列表)中的元素连接成一个字符串,可以指定一个连接符作为参数。

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

# 使用空字符串作为连接符连接列表中的元素
result = "".join(fruits)
print(result)
输出:苹果香蕉橙子葡萄
s = '123'
ls = [a for a in s] # ls:['1', '2', '3']
ls = [int(a) for a in s] # ls:[1, 2, 3]

Guess you like

Origin blog.csdn.net/weixin_46483785/article/details/132899833