当前位置:网站首页>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
边栏推荐
- Unity3d click events added to 3D objects in the scene
- 121. The best time to buy and sell stocks
- 如何在shell中实现 backspace
- PHP实现执行定时任务的几种思路详解
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
- Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
- Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
- Three. JS introductory learning notes 03: perspective projection camera
- Logback logging framework third-party jar package is available for free
猜你喜欢

Numpy -- data cleaning

神经网络c语言中的指针是怎么回事

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

Odoo集成Plausible埋码监控平台

Unity3d click events added to 3D objects in the scene

Logback logging framework third-party jar package is available for free

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models

Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
随机推荐
一个普通人除了去工厂上班赚钱,还能干什么工作?
AE learning 02: timeline
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Leetcode-231-2的幂
121. 买卖股票的最佳时机
Function: JS Click to copy content function
招标公告:盘锦市人民医院盘锦医院数据库维保项目
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
How to query the data of a certain day, a certain month, and a certain year in MySQL
Unity3D_ Class fishing project, bullet rebound effect is achieved
Communication mode between application program and MATLAB
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
How to determine whether the checkbox in JS is selected
Syntaxhighlight highlights the right scroll bar
C4D learning notes 1- animation - animation key frames
torch.numel作用
Continuous creation depends on it!
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
Wireless sensor networks -- ZigBee and 6LoWPAN
C4D learning notes 3- animation - animation rendering process case