Leetcode 136. 只出现一次的数字

136. 只出现一次的数字 - 力扣(LeetCode) (leetcode-cn.com)

 

思路 使用位运算^:

异或运算性质如下

1^1=0 
0^0=0
1^0=1
0^1=1 

 

所以我们只需要把数组中的元素遍历异或一遍,剩下的就是那个单独的数字

func singleNumber(nums []int) int {
    result:=0
    for _,item:=range nums{
        result^=item
    }
    return result
}

  

posted @ 2022-04-26 10:27  SoutherLea  阅读(15)  评论(0编辑  收藏  举报