当前位置:网站首页>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
边栏推荐
- 121. 买卖股票的最佳时机
- 过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
- U3D_ Infinite Bessel curve
- 应用程序和matlab的通信方式
- Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
- 谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
- 尤雨溪,来了!
- Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
猜你喜欢

分步式監控平臺zabbix

保证接口数据安全的10种方案

航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆

无线传感器网络--ZigBee和6LoWPAN

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

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

MySQL数据库基本操作-DQL-基本查询

深度之眼(六)——矩阵的逆(附:logistic模型一些想法)

What about the pointer in neural network C language
随机推荐
121. 买卖股票的最佳时机
Leetcode-231-2的幂
Three. JS introductory learning notes 11:three JS group composite object
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
SPI master rx time out中断
What else can an ordinary person do besides working in a factory to make money?
Iptables only allows the specified IP address to access the specified port
What about the pointer in neural network C language
numpy---基础学习笔记
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
PHP实现执行定时任务的几种思路详解
PHP实现微信小程序人脸识别刷脸登录功能
Unity3D_ Class fishing project, bullet rebound effect is achieved
How does geojson data merge the boundaries of regions?
Numpy --- basic learning notes
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Numpy -- epidemic data analysis case
强化实时数据管理,英方软件助力医保平台安全建设