美文网首页leetcode题解
【Leetcode】66—Plus One

【Leetcode】66—Plus One

作者: Gaoyt__ | 来源:发表于2019-07-20 16:45 被阅读0次
一、题目描述

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。
示例:

输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。

输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。
二、代码实现
class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        index = len(digits) - 1
        carry = 1
        while (index >= 0) and carry == 1:
            if digits[index] == 9: 
                digits[index] = 0
                carry = 1
            else:
                digits[index] = digits[index] + carry
                carry = 0
            index = index - 1
        if carry == 1: digits.insert(0, 1)
        return digits

相关文章

  • LeetCode 66-70

    66. Plus One[https://leetcode-cn.com/problems/plus-one/] ...

  • 66. Plus One

    66. Plus One 题目:https://leetcode.com/problems/plus-one/ 难...

  • LeetCode每日练习(66、724、189)

    66-加一[https://leetcode-cn.com/problems/plus-one/] 输入:digi...

  • LeetCode:66. 加一

    问题链接 66. 加一[https://leetcode-cn.com/problems/plus-one] 问题...

  • Leetcode-66 加一

    66. 加一[https://leetcode-cn.com/problems/plus-one/] 解题思路 1...

  • 66. 加一

    题目地址(66. 加一) https://leetcode.cn/problems/plus-one/[https...

  • LeetCode 66 [Plus One]

    原题 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。该数字按照大小进行排列,最大的数在列...

  • 【Leetcode】66—Plus One

    一、题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组...

  • leetcode:66. Plus One

    66. Plus One Description Given a non-empty array of digit...

  • Leetcode_66 Plus One

    给定一个非负整数组成的非空数组,给整数加一。 可以假设整数不包含任何前导零,除了数字0本身。 最高位数字存放在列表...

网友评论

    本文标题:【Leetcode】66—Plus One

    本文链接:https://www.haomeiwen.com/subject/bpeulctx.html