当前位置:网站首页>Leetcode-136-只出现一次的数(用异或来解答)
Leetcode-136-只出现一次的数(用异或来解答)
2022-07-07 13:53:00 【_春天_】
题目描述
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
示例 1:
输入: [2,2,1]
输出: 1
示例 2:
输入: [4,1,2,1,2]
输出: 4
Tips:用异或来解决
基础知识
异或的特性:
1.恒定律:A ^ 0 = A
2.归零率:A ^ A = 0
3.交换律:A ^ B = B ^ A
4.结合律:(A ^ B) ^ C = A ^ (B ^ C)
应用点
可以通过异或来判断两值是否相等:a ^ b == 0,则a与b相等。
用异或来消除出现两次的数
解题思路
利用异或的归零率,使所有出现过两次的数变为0,而只出现过一次的数与0运算还能保持本身。可以看下面这个例子。
假设所有的数组为:abcbcda
a ^ b ^ c ^ b ^ c ^ d ^ a
= a ^ a ^ b ^ b ^ c ^ c ^ d
= 0 ^ 0 ^ 0 ^ d
= d
代码
解法一:
我第一次的辣鸡解法,楞找。
时间复杂度高,最后一个测试用例没有通过。
def singleNumber(nums):
"""
:type nums: List[int]
:rtype: int
"""
validated = []
for i, num in enumerate(nums):
if num in validated:
continue
single = True
for j in range(len(nums) - i - 1):
print(num, nums[j+i+1])
if num == nums[j + i + 1]:
validated.append(num)
single = False
if single:
print(num)
break
解法二:优秀的算法:
def excellent_singleNumber(nums):
a = 0
for num in nums:
a ^= num
return a
第一眼看到这个算法的时候觉得太惊艳了……
边栏推荐
- Getting started with webgl (3)
- U3D_ Infinite Bessel curve
- It's different for rich people to buy a house
- Using eating in cocos Creator
- Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
- AB package details in unity (super detail, features, packaging, loading, manager)
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- LeetCode2_ Add two numbers
- Getting started with webgl (4)
猜你喜欢
torch.numel作用
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
AE learning 02: timeline
LeetCode3_ Longest substring without duplicate characters
分步式監控平臺zabbix
Write sequence frame animation with shader
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Annexb and avcc are two methods of data segmentation in decoding
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
随机推荐
Dotween -- ease function
Three. JS introductory learning notes 11:three JS group composite object
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
SPI master rx time out中断
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Write sequence frame animation with shader
SPI master RX time out interrupt
如何在shell中实现 backspace
Application example of infinite list [uigridview]
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
A link opens the applet code. After compilation, it is easy to understand
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
OpenGL common functions
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
asyncio 概念和用法
一大波开源小抄来袭
AB package details in unity (super detail, features, packaging, loading, manager)
OpenGL's distinction and understanding of VAO, VBO and EBO