mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
20 lines
392 B
Go
20 lines
392 B
Go
package leetcode
|
|
|
|
func singleNumberIII(nums []int) []int {
|
|
diff := 0
|
|
for _, num := range nums {
|
|
diff ^= num
|
|
}
|
|
// Get its last set bit (lsb)
|
|
diff &= -diff
|
|
res := []int{0, 0} // this array stores the two numbers we will return
|
|
for _, num := range nums {
|
|
if (num & diff) == 0 { // the bit is not set
|
|
res[0] ^= num
|
|
} else { // the bit is set
|
|
res[1] ^= num
|
|
}
|
|
}
|
|
return res
|
|
}
|