对多一位数字或者无数字输入的情况处理都统一了。 1publicclassSolution {2publicint[] plusOne(int[] digits) {3intlen =digits.length;4for(inti=len-1; i>=0; i--) {5intd =digits[i];6if(d<9) {7digits[i]++;8returndigits;9}else{10digits[i] = 0;11}12}13int[] res =newint[len+...
1publicclassSolution {2publicint[] plusOne(int[] digits) {3intlen=digits.length;4if(digits[len-1]<9){5digits[len-1]++;6returndigits;7}8booleanflag=true;9for(inti=len-1;i>=0;i--){10if(!flag){11break;12}13if(digits[i]==9){14digits[i]=0;15if(i==0){16int[] result=new...
注意最高位产生进位时(即最后 carry 不为 0 时),需要在结果数组前面加一个 carry 。 时间复杂度:O(n) 需要遍历计算全部 O(n) 个数位 空间复杂度:O(n) 最高位需要进位时,需要生产一个 O(n) 的数组 代码(Python3) class Solution: def plusOne(self, digits: List[int]) -> List[int]: # carry...
Can you solve this real interview question? Plus One - You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-t
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...
题目地址:https://leetcode-cn.com/problems/plus-one-linked-list/ 题目描述 用一个 非空 单链表来表示一个非负整数,然后将这个整数加一。 你可以假设这个整数除了 0 本身,没有任何前导的 0。 这个整数的各个数位按照高位在链表头部、低位在链表尾部的顺序排列。
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def plusOne(self, head: ListNode) -> ListNode: if not head: return head stack = [] cur = head while cur: stack.append(cur) cur = cur.next carry = 1 while stack: tmp = stack...
Parts of the problems don't provide C interface for solution, so I accomplished them with C++ Language. CompileCfiles using command: CompileC++files using command: g++ -std=c++11 -Wall src/bar.cpp -o bar OR You can build all the files usingmake(Use MinGW GCC and GNU Make on Windows)....
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 ...
Plus One 1 2 array Math 67 Add Binary 2 4 string Two Pointers Math 68 Text Justification 4 2 string 69 Sqrt(x) 4 4 Binary Search 70 Climbing Stairs 2 5 DP 71 Simplify Path 3 1 string Stack 72 Edit Distance 4 3 string DP 73 Set Matrix Zeroes 3 5 array 74 Search a 2D Matrix ...