当前位置:网站首页>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
边栏推荐
猜你喜欢
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
模仿企业微信会议室选择
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
What are compiled languages and interpreted languages?
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
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"?
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
AE learning 02: timeline
随机推荐
markdown公式编辑教程
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Use moviepy Editor clips videos and intercepts video clips in batches
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"?
Three. JS introductory learning notes 13: animation learning
Three. JS introductory learning notes 03: perspective projection camera
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
prometheus api删除某个指定job的所有数据
Shader_ Animation sequence frame
持续创作,还得靠它!
Three. JS introductory learning notes 15: threejs frame animation module
PHP实现微信小程序人脸识别刷脸登录功能
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Laravel 中config的用法
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
There are many ways to realize the pause function in JS
Performance measure of classification model