Leetcode 137. Single Number I/II/III
2024-10-13 08:33:02
Given an array of integers, every element appears twice except for one. Find that single one.
本题利用XOR的特性, X^0 = X, X^X = 0, 并且XOR满足交换律。
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
s = 0
for x in nums:
s= s^x return s
single number II/III可以用位操作。用Hash table也可以通过OJ
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dict = {}
for i in range(len(nums)):
if nums[i] not in dict:
dict[nums[i]] = 1
else:
dict[nums[i]] += 1 for word in dict:
if dict[word] == 1:
return word
最新文章
- 深入理解CSS定位中的偏移
- 基于HTML5 WebGL实现3D飞机叶轮旋转
- strlen() 函数
- Session的实现与存储
- Spring 定时任务 quartz的配置
- Constant is not finite! That's illegal. constant:inf'
- ExcelApplication 另存Excel的SaveAs函数
- Android L动画入门
- LeetCode OJ 85. Maximal Rectangle
- 巧妙利用ToArray()函数移除集合中的元素
- P2P应用中的NAT穿透问题
- spring框架应用系列二:component-scan自动扫描注册装配
- day319 1、正则表达式的定义及使用 2、Date类的用法 3、Calendar类的用法
- Contest2075 - 湖南多校对抗(csu1576)大数 Catalan Square
- Memento Mori (二维前缀和 + 枚举剪枝)
- POPUP_TO_CONFIRM的使用方法
- Mac 系统上安装Lua和SubmlimeText 编译器
- jsp jsp的基本语法
- 每天一个linux命令(1):find命令之exec
- PAT 1059 Prime Factors[难]