-
Notifications
You must be signed in to change notification settings - Fork 26
/
0066-plus-one.cpp
72 lines (62 loc) · 2.36 KB
/
0066-plus-one.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Problem: LeetCode 66 - Plus One
Description:
Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except for the number 0 itself.
Intuition:
To increment a number represented as an array of digits, we need to add one to the last digit and handle any carry that may occur.
Approach:
1. Start from the end of the digits array.
2. Add one to the last digit.
3. If the digit becomes 10 (carry occurs), set it to 0 and move to the next digit.
4. Continue this process until there is no more carry or we reach the beginning of the digits array.
5. If there is still a carry after processing all digits, it means the original number was all nines, and we need to add a new digit at the beginning of the array with a value of 1.
Time Complexity:
The time complexity is O(n), where n is the number of digits in the array. In the worst case, we may need to carry the addition all the way to the beginning of the array.
Space Complexity:
The space complexity is O(1) as we are modifying the input array in place and not using any additional data structures.
*/
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int n = digits.size();
// Start from the end and add one to the last digit
digits[n - 1] += 1;
// Handle any carry
for (int i = n - 1; i > 0; i--) {
if (digits[i] == 10) {
digits[i] = 0;
digits[i - 1] += 1;
} else {
break; // No more carry, exit the loop
}
}
// If there is still a carry, add a new digit at the beginning of the array
if (digits[0] == 10) {
digits[0] = 0;
digits.insert(digits.begin(), 1);
}
return digits;
}
};
/*
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
for(int i=digits.size()-1; i>=0; i--){
digits[i]++;
if(digits[i]==10){
digits[i]=0;
}
else{
break;
}
}
if(digits[0]==0){
digits.insert(digits.begin(), 1);
}
return digits;
}
};
*/