leetcode66 python plus one

Given a non -empty array of non -negative integers , add one to the number and return a new array.

The highest digit is stored in the first position of the array, and each element in the array stores only one number.

You can assume that this integer will not start with zero except for the integer 0.

Example 1:

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

Example 2:

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

python code

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        # convert array to number
        intNum = 0
        for i in range(len(digits)):
            intNum=intNum*10+digits[i]
        intNum + = 1
        # convert numbers to characters
        strNum=str(intNum)
        # convert characters to array
        res=[]
        for i in range(len(strNum)):
            res.append(int(strNum[i]))
        return res



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325729398&siteId=291194637