Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6eb1f47

Browse files
Fix integer overflow in LC260. Single Number III
- Changed `int` to `long long` for variable `s` to prevent overflow when handling `INT_MIN` (`-2147483648`). - Updated `t` to use `long long` to ensure correctness in bit manipulation. - Fixes issue with new test case: `nums = [1,1,0,-2147483648]`, where `int` overflowed due to `s & (s-1)`.
1 parent 1c25251 commit 6eb1f47

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

‎Bit_Manipulation/260.Single-Number-III/260.Single-Number-III.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Solution {
22
public:
33
vector<int> singleNumber(vector<int>& nums)
44
{
5-
int s = 0;
5+
longlong s = 0;
66
for (auto n:nums) s = s^n; // i.e. a^b
7-
int t = s^(s&(s-1)); // only keep the rightmost set bit
7+
longlong t = s^(s&(s-1)); // only keep the rightmost set bit
88
int a = 0, b = 0;
99
for (auto n:nums)
1010
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /