首页 > 其他 > 详细

66. Plus One

时间:2020-07-15 00:43:04      阅读:41      评论:0      收藏:0      [点我收藏+]

Given a non-empty array of digits representing a non-negative integer, increment 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 contains a single digit.

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

数组表示大整数,求大整数+1后的数组

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        n = len(digits)
        ans = [0] * n
        carry = 1
        for i in range(n - 1, -1, -1):
            num = digits[i] + carry
            ans[i] = num % 10
            carry = num // 10
        if carry:
            ans.insert(0, carry)
        return ans

 

66. Plus One

原文:https://www.cnblogs.com/whatyouthink/p/13301780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!