当前位置:网站首页>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
边栏推荐
- How to implement backspace in shell
- 山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
- 无线传感器网络--ZigBee和6LoWPAN
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- 尤雨溪,来了!
- Leetcode-136-只出现一次的数(用异或来解答)
- How to query the data of a certain day, a certain month, and a certain year in MySQL
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
- TS as a general cache method
猜你喜欢
SPI master RX time out interrupt
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"?
How does geojson data merge the boundaries of regions?
JS array foreach source code parsing
95. (cesium chapter) cesium dynamic monomer-3d building (building)
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
Odoo integrated plausible embedded code monitoring platform
Three. JS introductory learning notes 18: how to export JSON files with Blender
10 schemes to ensure interface data security
神经网络c语言中的指针是怎么回事
随机推荐
Description of vs common shortcut keys
Application example of infinite list [uigridview]
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
prometheus api删除某个指定job的所有数据
TS typescript type declaration special declaration field number is handled when the key key
JS array foreach source code parsing
Xcode Revoke certificate
MySQL数据库基本操作-DQL-基本查询
nodejs package. JSON version number ^ and~
What about the pointer in neural network C language
Logback logging framework third-party jar package is available for free
统计学习方法——感知机
Three singleton modes of unity (hungry man, lazy man, monobehavior)
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
SPI master rx time out中断
Odoo integrated plausible embedded code monitoring platform
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
121. The best time to buy and sell stocks