当前位置:网站首页>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
边栏推荐
- Numpy --- basic learning notes
- Markdown formula editing tutorial
- There are many ways to realize the pause function in JS
- Three. JS introductory learning notes 11:three JS group composite object
- Three singleton modes of unity (hungry man, lazy man, monobehavior)
- Regular expression string
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- 如何在shell中实现 backspace
- Vite path alias @ configuration
- thinkphp3.2.3中设置路由,优化url
猜你喜欢
Leetcode-231-2的幂
Logback日志框架第三方jar包 免费获取
尤雨溪,来了!
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
神经网络c语言中的指针是怎么回事
Postman generate timestamp, future timestamp
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
[wechat applet] Chapter (5): basic API interface of wechat applet
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
Three. JS introductory learning notes 03: perspective projection camera
随机推荐
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
企业级日志分析系统ELK
What else can an ordinary person do besides working in a factory to make money?
Unity3d click events added to 3D objects in the scene
Markdown formula editing tutorial
Three. JS introductory learning notes 15: threejs frame animation module
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
强化实时数据管理,英方软件助力医保平台安全建设
10 schemes to ensure interface data security
统计学习方法——感知机
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Logback logging framework third-party jar package is available for free
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
It's different for rich people to buy a house
nodejs package. JSON version number ^ and~
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
如何在shell中实现 backspace
A JS script can be directly put into the browser to perform operations