当前位置:网站首页>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
边栏推荐
- Dotween -- ease function
- ThinkPHP URL 路由简介
- C4D learning notes 3- animation - animation rendering process case
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
- MySQL中, 如何查询某一天, 某一月, 某一年的数据
- 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"?
- AE learning 01: AE complete project summary
- 尤雨溪,来了!
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
- C4D learning notes 1- animation - animation key frames
猜你喜欢

Vs tool word highlight with margin

torch. Numel action

Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

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

分步式监控平台zabbix

Description of vs common shortcut keys

Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?

【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
随机推荐
Mysql database basic operation DQL basic query
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
AE learning 01: AE complete project summary
强化实时数据管理,英方软件助力医保平台安全建设
Regular expression string
iptables只允许指定ip地址访问指定端口
通知Notification使用全解析
It's different for rich people to buy a house
JS array foreach source code parsing
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
Function: JS Click to copy content function
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
Application example of infinite list [uigridview]
Description of vs common shortcut keys
无线传感器网络--ZigBee和6LoWPAN
Logback日志框架第三方jar包 免费获取
AE learning 02: timeline
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models