Skip to content

Commit 2998feb

Browse files
committed
fix error related to integer overflow
1 parent 439c0cc commit 2998feb

File tree

1 file changed

+2
-22
lines changed

1 file changed

+2
-22
lines changed

c++/single-number-iii.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ std::vector<int> singleNumber(std::vector<int>& nums) {
3737

3838
return result;
3939
}
40-
41-
int main() {
42-
std::vector<int> nums = {1, 2, 1, 3, 2, 5};
43-
std::vector<int> result = singleNumber(nums);
44-
std::cout << "The numbers that appear once are: ";
45-
for (int num : result) {
46-
std::cout << num << " ";
47-
}
48-
return 0;
49-
}
5040
```
5141
5242
### Time Complexity:
@@ -82,7 +72,7 @@ std::vector<int> singleNumber(std::vector<int>& nums) {
8272
}
8373
8474
// Find rightmost set bit different between two unique numbers
85-
int rightmost_set_bit = xor_all & -xor_all;
75+
unsigned int rightmost_set_bit = xor_all & -static_cast<unsigned int>(xor_all);
8676
8777
int num1 = 0, num2 = 0;
8878
@@ -97,21 +87,11 @@ std::vector<int> singleNumber(std::vector<int>& nums) {
9787
9888
return {num1, num2};
9989
}
100-
101-
int main() {
102-
std::vector<int> nums = {1, 2, 1, 3, 2, 5};
103-
std::vector<int> result = singleNumber(nums);
104-
std::cout << "The numbers that appear once are: ";
105-
for (int num : result) {
106-
std::cout << num << " ";
107-
}
108-
return 0;
109-
}
11090
```
111-
11291
### Time Complexity:
11392
- O(n), where n is the number of elements in the vector. Each element is processed a constant number of times.
11493

11594
### Space Complexity:
11695
- O(1), as we are using fixed extra space regardless of input size.
11796

97+

0 commit comments

Comments
 (0)