image.png
解题思路:
- 按位异或,同一个数字出现两次,表示该位上异或两次,该位的值还是0;
- 出现一次的值即是异或的结果。
Python3代码:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
s = 0
for num in nums:
s^=num
return s
image.png
解题思路:
Python3代码:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
s = 0
for num in nums:
s^=num
return s
本文标题:LeetCode-136-只出现一次的数字
本文链接:https://www.haomeiwen.com/subject/jldwwktx.html
网友评论