当前位置:网站首页>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
边栏推荐
- Postman generate timestamp, future timestamp
- MySQL中, 如何查询某一天, 某一月, 某一年的数据
- TS as a general cache method
- Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
- 持续创作,还得靠它!
- 航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
- Continuous creation depends on it!
- 招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
- Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
- Numpy -- data cleaning
猜你喜欢
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Numpy --- basic learning notes
Three. JS introductory learning notes 04: external model import - no material obj model
Unity3D_ Class fishing project, bullet rebound effect is achieved
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Unity drawing plug-in = = [support the update of the original atlas]
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
Dotween -- ease function
随机推荐
Three singleton modes of unity (hungry man, lazy man, monobehavior)
Use moviepy Editor clips videos and intercepts video clips in batches
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Logback logging framework third-party jar package is available for free
Unity3D_ Class fishing project, bullet rebound effect is achieved
C4D learning notes 2- animation - timeline and time function
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Odoo集成Plausible埋码监控平台
Notification uses full resolution
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Simple understanding and application of TS generics
Unity3d click events added to 3D objects in the scene
Mysql database basic operation DQL basic query
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
What about the pointer in neural network C language
Align individual elements to the right under flex layout
You Yuxi, coming!
保证接口数据安全的10种方案