当前位置:网站首页>Leetcode 0136. numbers that appear only once: XOR
Leetcode 0136. numbers that appear only once: XOR
2022-07-25 23:35:00 【Tisfy】
【LetMeFly】136. A number that appears only once : Exclusive or
Force button topic link :https://leetcode.cn/problems/single-number/
Given a Non empty An array of 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
Method 1 : Exclusive or
Important properties of XOR :
- a ⊕ b ⊕ b = a a\oplus b\oplus b=a a⊕b⊕b=a
- 0 ⊕ a = a 0\oplus a = a 0⊕a=a
- The XOR result is independent of the XOR order
therefore , We use it 0 0 0 Start , XOR every number , The end result is the answer .
( Except that the answer appeared twice , 0 0 0 An XOR counts twice or 0 0 0; The answer only appears once , 0 0 0 The XOR answer once equals the answer )
- Time complexity O ( n ) O(n) O(n), among n n n It's the number of elements
- Spatial complexity O ( 1 ) O(1) O(1)
AC Code
C++
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = 0;
for (int& t : nums) {
ans ^= t;
}
return ans;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125978508
边栏推荐
- XXE&XML-外部实体注入-利用和绕过
- What is the difference between hot deployment and hot loading?
- Docker 安装 Redis-5.0.12(远程访问)
- PyTorch的数据输入格式要求及转换
- Which securities company should a novice choose to open an account? Is it safe?
- [test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
- [JUC] concurrent keyword volatile
- Matchmaker's words
- Ffmpeg first learning (only for coding)
- TS class
猜你喜欢

Anti shake and throttling

The VM session was closed before any attempt to power it on

SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据

ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用

POI特效 市场调研

获取马蜂窝酒店数据

图的遍历-DFS,BFS(代码详解)

数组中重复的数字

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

【MUDUO】EventLoop事件循环
随机推荐
S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
[JUC] concurrent keyword volatile
XxE & XML external entity injection utilization and bypass
Swap, move, forward, exchange of utility component learning
Mongodb query and projection operators
[QNX hypervisor 2.2 user manual]9.7 generate
红娘的话
2022 Niuke multi School Game 2
Data broker understanding
Computed and watch listening properties
动态内存管理
The VM session was closed before any attempt to power it on
Solution of phpstudy service environment 80 port occupied by process system under Windows
152. 乘积最大子数组-动态规划
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
npm+模块加载机制
Classes and objects (3)
WebMvcConfigurationSupport
Inheritance (the child constructor inherits the attributes in the parent constructor)
Regular expression (user name form verification / verification of landline number / regular replacement)