From 6eb1f47e40d50eba73ac822f0ff1364c57dbb66c Mon Sep 17 00:00:00 2001 From: Eric Chou Date: 2025年3月12日 02:37:22 +0800 Subject: [PATCH] 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)`. --- .../260.Single-Number-III/260.Single-Number-III.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bit_Manipulation/260.Single-Number-III/260.Single-Number-III.cpp b/Bit_Manipulation/260.Single-Number-III/260.Single-Number-III.cpp index d5a308b60..bbfea14a2 100644 --- a/Bit_Manipulation/260.Single-Number-III/260.Single-Number-III.cpp +++ b/Bit_Manipulation/260.Single-Number-III/260.Single-Number-III.cpp @@ -2,9 +2,9 @@ class Solution { public: vector singleNumber(vector& nums) { - int s = 0; + long long s = 0; for (auto n:nums) s = s^n; // i.e. a^b - int t = s^(s&(s-1)); // only keep the rightmost set bit + long long t = s^(s&(s-1)); // only keep the rightmost set bit int a = 0, b = 0; for (auto n:nums) {

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