当前位置:网站首页>Leetcode-136- number that appears only once (solve with XOR)
Leetcode-136- number that appears only once (solve with XOR)
2022-07-07 16:14:00 【_ Spring_】
Title Description
Given an array of non-empty integers , Except that an element only appears once , Each of the other elements occurs twice . Find the element that only appears once .
explain :
Your algorithm should have linear time complexity . Can you do this without using extra space ?
Example 1:
Input : [2,2,1]
Output : 1
Example 2:
Input : [4,1,2,1,2]
Output : 4
Tips: Solve with XOR
Basic knowledge of
The characteristic of XOR :
1. Law of constancy :A ^ 0 = A
2. Zero rate :A ^ A = 0
3. Commutative law :A ^ B = B ^ A
4. Associative law :(A ^ B) ^ C = A ^ (B ^ C)
Application point
You can judge whether the two values are equal by XOR :a ^ b == 0, be a And b equal .
Use XOR to eliminate numbers that appear twice
Their thinking
Use the zeroing rate of XOR , Make all the numbers that appear twice become 0, And the number and... That only appear once 0 The operation can still keep itself . Let's look at the following example .
Let's say all arrays are :abcbcda
a ^ b ^ c ^ b ^ c ^ d ^ a
= a ^ a ^ b ^ b ^ c ^ c ^ d
= 0 ^ 0 ^ 0 ^ d
= d
Code
Solution 1 :
My first spicy chicken solution , Leng find .
High time complexity , The last test case failed .
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
Solution 2 : Excellent algorithm :
def excellent_singleNumber(nums):
a = 0
for num in nums:
a ^= num
return a
It was amazing when I first saw this algorithm ……
Reference resources :
Leetcode-136- A number that appears only once
边栏推荐
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
- Syntaxhighlight highlights the right scroll bar
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- [wechat applet] Chapter (5): basic API interface of wechat applet
- Laravel5.1 路由 -路由分组
- Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
- laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
- Vs tool word highlight with margin
- Function: JS Click to copy content function
猜你喜欢

Continuous creation depends on it!

保证接口数据安全的10种方案

Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
![Unity drawing plug-in = = [support the update of the original atlas]](/img/b0/92114ffb1f168a1f27125db46c6797.jpg)
Unity drawing plug-in = = [support the update of the original atlas]

A wave of open source notebooks is coming

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
Notification uses full resolution

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
随机推荐
Laravel5.1 路由 -路由分组
强化实时数据管理,英方软件助力医保平台安全建设
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
分类模型评价标准(performance measure)
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Logback logging framework third-party jar package is available for free
Use moviepy Editor clips videos and intercepts video clips in batches
SPI master rx time out中断
Syntaxhighlight highlights the right scroll bar
Leetcode-136-只出现一次的数(用异或来解答)
AE learning 01: AE complete project summary
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
无线传感器网络--ZigBee和6LoWPAN
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
分步式监控平台zabbix
markdown公式编辑教程
121. The best time to buy and sell stocks
A link opens the applet code. After compilation, it is easy to understand
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file