首页 > 其他分享 >LeetCode 第66题. 加1

LeetCode 第66题. 加1

时间:2023-07-20 22:05:48浏览次数:23  
标签:digits 数组 示例 len 整数 66 LeetCode 进位

题目:

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入:digits = [1,2,3]
输出:[1,2,4]

示例 2:

输入:digits = [4,3,2,1]
输出:[4,3,2,2]

示例 3:

输入:digits = [0]
输出:[1]
class Solution(object):
    def plusOne(self, digits):
        d_len = len(digits)
        if digits[d_len-1]<9:
            digits[d_len-1] = digits[d_len-1]+1
            return digits
        else:
            i = d_len
            while i > 0:
                if digits[i-1] == 9:
                    digits[i-1] = 0
                    i -= 1
                    if i == 0:
                        for j in range(d_len):
                            if j == 0:
                                digits[j]=1
                            else:
                                digits[j]=0
                        digits.append(0)
                        return digits
                else:
                    digits[i-1] = digits[i-1]+1
                    return digits

这是按着自己的想法来写的,主要判断最低位是否为9,如果为9就要进位。进位的话就要考虑进位后会不会又导致进位。有一种可能就是要增加一位,比如9加1就要进位为10,加了一位。

LeetCode 第66题. 加1_git

标签:digits,数组,示例,len,整数,66,LeetCode,进位
From: https://blog.51cto.com/u_16123878/6792230

相关文章

  • leetcode-1518-easy
    WaterBottlesTherearenumBottleswaterbottlesthatareinitiallyfullofwater.YoucanexchangenumExchangeemptywaterbottlesfromthemarketwithonefullwaterbottle.Theoperationofdrinkingafullwaterbottleturnsitintoanemptybottle.......
  • Codility / LeetCode的重要性与注意事项
    Codility/Leetcode不只会针对回答内容给出最终分数,也会一并记录解题的过程供面试官参考;相较于现场考试,Codility/Leetcode可以省下更多时间,也能让求职者在最熟悉的环境发挥实力。 进行测验前先查看Codility/LeetcodeFAQ,并完成demo题。可试着多做几题练习题,能全部做......
  • [LeetCode] 2268. Minimum Number of Keypresses
    Youhaveakeypadwith 9 buttons,numberedfrom 1 to 9,eachmappedtolowercaseEnglishletters.Youcanchoosewhichcharacterseachbuttonismatchedtoaslongas:All26lowercaseEnglishlettersaremappedto.Eachcharacterismappedtoby exact......
  • CF1662C European Trip
    CF1662CEuropeanTrip没有限制怎么做?邻接矩阵\(k\)次方。有限制?设\(A\)为邻接矩阵,\(I\)为单位矩阵,\(deg_u\)为\(u\)的度数,步数为\(k\)是的答案矩阵\(R_k\),即\(R_k[u][v]\)应该表示\(u\tov\)长度为\(k\)的合法路径条数。如果我们知道了\(R_1,R_2,\dots,R_......
  • [LeetCode] 2486. Append Characters to String to Make Subsequence
    Youaregiventwostrings s and t consistingofonlylowercaseEnglishletters.Return theminimumnumberofcharactersthatneedtobeappendedtotheendof s sothat t becomesa subsequence of s.A subsequence isastringthatcanbederived......
  • LeetCode 1201. Ugly Number III 数学+二分答案
    Anuglynumberisapositiveintegerthatisdivisibleby\(a\),\(b\),or\(c\).Givenfourintegers\(n\),\(a\),\(b\),and\(c\),returnthe\(n\)thuglynumber.Solution考虑如何二分答案,假设一个函数\(f(num,a,b,c)\)会得到\([1,num]\)中uglynumb......
  • [刷题记录Day4]Leetcode链表专题
    No.1题目两两交换链表中的节点思路模拟类型题目两个节点前后交换,同时记住原来的下一个节点虚拟头节点代码public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(-1, head); ListNode cur = dummyHead; while (cur.next != ......
  • LeetCode 875. Koko Eating Bananas 二分答案
    Kokolovestoeatbananas.Thereare\(n\)pilesofbananas,the\(i\)thpilehas\(piles[i]\)bananas.Theguardshavegoneandwillcomebackinhhours.Kokocandecideherbananas-per-houreatingspeedofk.Eachhour,shechoosessomepileofb......
  • CS5466 Type-c to HDMI2.1_8K拓展坞方案芯片|低成本替代GSV6201方案
    GSV6201是一款高性能、低功耗、高性能的,USBType-C备用模式显示端口1.4至HDMI2.1转换器。通过集成增强型微控制器,GSV6201创造了一个经济高效的解决方案提供了上市时间优势。显示端口接收机支持高达32.4Gbps(HBR3,4通道)和HDMI发射机支持高达48Gbps(FRL,12G4Lane)。集成PowerDelivery3......
  • LeetCode 1011. Capacity To Ship Packages Within D Days 二分答案
    Aconveyorbelthaspackagesthatmustbeshippedfromoneporttoanotherwithindaysdays.Theithpackageontheconveyorbelthasaweightof\(weights[i]\).Eachday,weloadtheshipwithpackagesontheconveyorbelt(intheordergivenby\(wei......