LeetCode练题——66. Plus one

1、题目

66. Plus One——easy

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

2、我的解法

 1 # -*- coding: utf-8 -*-
 2 # @Time    : 2020/2/8 17:04
 3 # @Author  : SmartCat0929
 4 # @Email   : [email protected]
 5 # @Link    : https://github.com/SmartCat0929
 6 # @Site    : 
 7 # @File    : 66.PLus one.py
 8 
 9 from typing import List
10 class Solution:
11     def plusOne(self, digits: List[int]) -> List[int]:
12         res = 0
13         for count, i in enumerate(reversed(digits)): # 第一步,把列表转化成整数
14             res += i * (10 ** count)
15         result=res+1                         # 第二步,对生成的整数进行 +1 运算
16         b = [int(x) for x in str(result)]  #   第三步,转化成列表—————采用列表生成式的方法
17         return b
18 print(Solution().plusOne([1,2,2,9]))

猜你喜欢

转载自www.cnblogs.com/Smart-Cat/p/12284119.html