Skip to content

Commit 2816553

Browse files
Update and rename PlusOne.cpp to easy-66-plus_one.cpp
1 parent 41a9bd0 commit 2816553

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

Codes/PlusOne.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

Codes/easy-66-plus_one.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer.
2+
3+
// The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
4+
5+
// Increment the large integer by one and return the resulting array of digits.
6+
7+
8+
9+
// Example 1:
10+
11+
// Input: digits = [1,2,3]
12+
// Output: [1,2,4]
13+
// Explanation: The array represents the integer 123.
14+
// Incrementing by one gives 123 + 1 = 124.
15+
// Thus, the result should be [1,2,4].
16+
17+
// Example 2:
18+
19+
// Input: digits = [4,3,2,1]
20+
// Output: [4,3,2,2]
21+
// Explanation: The array represents the integer 4321.
22+
// Incrementing by one gives 4321 + 1 = 4322.
23+
// Thus, the result should be [4,3,2,2].
24+
25+
// Example 3:
26+
27+
// Input: digits = [9]
28+
// Output: [1,0]
29+
// Explanation: The array represents the integer 9.
30+
// Incrementing by one gives 9 + 1 = 10.
31+
// Thus, the result should be [1,0].
32+
33+
34+
class Solution {
35+
public:
36+
vector<int> plusOne(vector<int>& digits) {
37+
int n=digits.size()-1;
38+
while(n>=0){
39+
if(n==digits.size()-1){
40+
digits[n]=digits[n]+1;
41+
}
42+
if(digits[n]==10){
43+
digits[n]=0;
44+
if(n!=0){
45+
digits[n-1]+=1;
46+
}
47+
if(n==0){
48+
digits.insert(digits.begin(),1);
49+
}
50+
}
51+
n--;
52+
}
53+
return digits;
54+
}
55+
};

0 commit comments

Comments
 (0)