题: 有一堆数字,如果除了一个数字以外,其他数字都出现了两次,那么如何找到出现一次的数字?
利用 ^ 二进位异或 运算符
func findTheOnlyNumber(nums []int) (res int) {
for index, num := range nums {
if index == 0 {
res = num
} else {
res = res ^ num
}
}
return
}
利用 ^ 二进位异或 运算符
func findTheOnlyNumber(nums []int) (res int) {
for index, num := range nums {
if index == 0 {
res = num
} else {
res = res ^ num
}
}
return
}