python实现leetcode之136. 只出现一次的数字
2021-10-12 本文已影响0人
深圳都这么冷
解题思路
从头到尾异或,出现两次的都被异或为0,只剩下答案
136. 只出现一次的数字
代码
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = 0
for n in nums:
ans ^= n
return ans
![](https://img.haomeiwen.com/i4291429/14b8fba3da7aa70e.png)