当前位置:网站首页>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
边栏推荐
- thinkphp3.2.3中设置路由,优化url
- Introduction to pyGame games
- A JS script can be directly put into the browser to perform operations
- numpy--疫情数据分析案例
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- MySQL数据库基本操作-DQL-基本查询
- Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
- torch. Numel action
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
- 融云斩获 2022 中国信创数字化办公门户卓越产品奖!
猜你喜欢
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Sysom case analysis: where is the missing memory| Dragon lizard Technology
torch.numel作用
分步式監控平臺zabbix
企业级日志分析系统ELK
SPI master rx time out中断
AE learning 02: timeline
Logback logging framework third-party jar package is available for free
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
随机推荐
Syntaxhighlight highlights the right scroll bar
ThinkPHP URL 路由简介
How does geojson data merge the boundaries of regions?
Function: JS Click to copy content function
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Application example of infinite list [uigridview]
C4D learning notes 2- animation - timeline and time function
torch. Numel action
Numpy -- data cleaning
hellogolang
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
神经网络c语言中的指针是怎么回事
Markdown formula editing tutorial
You Yuxi, coming!
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
C4D learning notes 1- animation - animation key frames
torch.numel作用
Odoo集成Plausible埋码监控平台