对多一位数字或者无数字输入的情况处理都统一了。 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+1];14res[0] = 1;15returnres;16}17}
模拟运算过程,从低位数字到高位数字计算,新建一个变量记录进位情况。 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] = ...
注意最高位产生进位时(即最后 carry 不为 0 时),需要在结果数组前面加一个 carry 。 时间复杂度:O(n) 需要遍历计算全部 O(n) 个数位 空间复杂度:O(n) 最高位需要进位时,需要生产一个 O(n) 的数组 代码(Python3) class Solution: def plusOne(self, digits: List[int]) -> List[int]: # carry...
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...
# 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...
题目地址:https://leetcode-cn.com/problems/plus-one-linked-list/ 题目描述 用一个 非空 单链表来表示一个非负整数,然后将这个整数加一。 你可以假设这个整数除了 0 本身,没有任何前导的 0。 这个整数的各个数位按照高位在链表头部、低位在链表尾部的顺序排列。
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 Go 43.3% Easy 0067 Add Binary Go 51.3% Easy 0068 Text Justification 36.6% Hard 0069 Sqrt(x) Go 37.0% Easy 0070 Climbing Stairs Go 51.7% Easy 0071 Simplify Path Go 39.2% Medium 0072 Edit Distance 52.6% Hard 0073 Set Matrix Zeroes Go 49.9% Medium 0074 Search ...
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 ...
0066 Plus One Go 43.0% Easy 0067 Add Binary Go 45.2% Easy 0068 Text Justification 27.7% Hard 0069 Sqrt(x) Go 33.9% Easy 0070 Climbing Stairs Go 47.8% Easy 0071 Simplify Path Go 32.6% Medium 0072 Edit Distance 44.8% Hard 0073 Set Matrix Zeroes 43.1% Medium 0074 Search a 2D...