模拟运算过程,从低位数字到高位数字计算,新建一个变量记录进位情况。 1classSolution {2public:3vector<int> plusOne(vector<int> &digits) {4intlen =digits.size();5intsig =0;6digits[len-1] +=1;7for(inti=len-1; i>=0; --i) {8digits[i] +=sig;9sig = digits[i] /10;10digits[i] = ...
Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 解题思路:这只一个flag标志位就可以了。 代码: class Solution: # @param digits, a list of integer digits # @ret...
日期 题目地址:https://leetcode-cn.com/problems/plus-one-linked-list/ 题目描述 用一个 非空 单链表来表示一个非负整数,然后将这个整数加一。 你可以假设这个整数除了 0 本身,没有任何前导的 0。 这个整数的各个数位按照高位在链表头部、低位在链表尾部的顺序排列。 示例: 输入: [1,2,3...
思路一:链表转成列表 # Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = NoneclassSolution:defplusOne(self,head:ListNode)->ListNode:importfunctoolsres=[]cur=headwhilecur:res.append(cur.val)cur=cur.nextnum=functools.reduce(lambdax,y...
classSolution:defplusOne(self,digits):""" :type digits: List[int] :rtype: List[int] """resStr="";res=0;resList=[]foriindigits:resStr+=str(i)res=str(int(resStr)+1)foriinres:resList.append(int(i))returnresList 2.8 移动零 ...
public class Solution { public int[] plusOne(int[] digits) { for (int i=digits.length-1; i>=0; i--) { if (digits[i]==9) { digits[i]=0; continue; } else { digits[i] += 1; return digits; } } // digits[0]==0 int[] newDigits = new int[digits.length+1]; newDigits...
0066-plus-one Time: 4 ms (25.77%), Space: 8.8 MB (6.67%) - LeetHub Aug 21, 2023 0079-word-search Time: 621 ms (61.96%), Space: 8 MB (71.93%) - LeetHub Jul 26, 2023 0090-subsets-ii Attach NOTES - LeetHub Jul 26, 2023 ...
66-plus-one 669-trim-a-binary-search-tree 678-valid-parenthesis-string 68-text-justification 69-sqrtx 695-max-area-of-island 713-subarray-product-less-than-k 724-find-pivot-index 726-number-of-atoms 728-self-dividing-numbers 73-set-matrix-zeroes 739-daily-temperatures 74-...
C++ primer plus 过了一遍,今天做了 LeetCode 前几题,感觉有点吃力,得在草稿上写的明明白白才能理清思路。听说 LeetCode…显示全部 关注者12,931 被浏览5,461,520 关注问题写回答 邀请回答 好问题 299 16 条评论 分享 ...
LeetCode Big O Notebookfor a user that activelyshare high-quality solution posts. LeetCoinsfor users that give high qualityupvote/discuss! 📌 Missed a challenge? Use the Time Travel Ticket! Do you have experience trying to keep a streak but failed because you missed one of the challenges?