当前位置:网站首页>Leetcode -- 136. a number that appears only once
Leetcode -- 136. a number that appears only once
2022-07-24 01:03:00 【JiawenZhang97】
subject
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
Own solution
- use set aggregate Storage nums;
- If the addition fails , Then the element already exists in the collection ,remove;
- There is only one element left in the final set ;
- ( The time complexity is O(n), Does not meet the requirements of the topic )
class Solution {
public int singleNumber(int[] nums) {
int result = -1;
Set set = new HashSet();
for (int num : nums){
boolean flag = set.add(num);
if (flag == false){
set.remove(num);
}
}
for (Object ss : set){
result = (int) ss;
}
return result;
}
}
Simple solution
For this question , XOR operation can be used ⊕.
- XOR has three properties
- Any number and 0 Do exclusive or operations , The result is still the original number , namely a ⊕ 0 = a.
- Any number is XOR with itself , The result is 0, namely a ⊕ a = 0.
- XOR operation satisfies the exchange law and the combination law , namely a ⊕ b ⊕ a = b ⊕ a ⊕ a = b ⊕ (a ⊕ a) = b ⊕ 0 = b
- step
- Let's say there's... In the array 2m+1 Number , Among them is m Two times each , A number appears once .
- Make a1、a2、…、am For two times m Number ,a (m+1) It's a number that appears once .
- According to the nature 3, The XOR result of all elements in an array can always be written as follows :
(a1⊕a1) ⊕ (a2⊕a2) ⊕ ... ⊕ (am⊕am) ⊕ a(m+1) - According to the nature 2 And nature 1, The above formula can be simplified and calculated to obtain the following results :
0 ⊕ 0 ⊕ ... ⊕ 0 ⊕ a(m+1) = a(m+1) - therefore , The XOR result of all elements in the array is the number that appears only once in the array .
class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for (int num : nums) {
single ^= num;
}
return single;
}
}
边栏推荐
- Thread pool summary
- OSPF experiment
- Basic exercises of C language for beginners
- C language force deduction question 53 of the largest subarray sum. Dynamic programming and divide and conquer
- Solve the problem that MySQL inserts Chinese garbled code into the table
- [the 83rd fortnight of leetcode]
- Selection method of geometric objects in Creo 9.0
- vim常用命令
- Memory forensics nssctf otterctf 2018 (replay)
- The way to access global variables in multi-source file mode (extern usage)
猜你喜欢

Kubernetes deployment dashboard (visual interface)

1000 okaleido tiger launched binance NFT, triggering a rush to buy

Idea hot deployment (hot load)

Testers who have been employed for 3 months are facing employment confirmation. Leaders: 1 year of work experience is packaged into 5 years, and the probation period is eliminated

Sublime text 3 汉化+添加常用插件

Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
![[the 83rd fortnight of leetcode]](/img/41/411dc235a34f9dde06a41323628648.png)
[the 83rd fortnight of leetcode]

Create a self signed certificate to digitally sign exe files

C language database: detailed description. Use the student management system to understand the operation of the database, which is simple and easy to understand.

【LeetCode第 83 场双周赛】
随机推荐
Socket basic knowledge and various usage scenarios
docker mysql
Group chat room based on UDP
C language: explain in detail the two local communication methods based on TCP and UDP
Create a self signed certificate to digitally sign exe files
Three usages of synchronized keywords in vernacular
Implementation of singleton mode and prevention of reflection and serialization
Introduction to QT (2.1 the first procedure for the beginning of QT)
[reply] about the fact that I chose the wrong technology at the wrong time
MySQL interview questions
Deep understanding of collaborative process
[the 83rd fortnight of leetcode]
freemarker
Use of crawler request library 2
SQL CASE 多条件用法
For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser
Creo 9.0 mouse button operation for model observation
The winverifytrust call returned 80096005 error. The timestamp signature or certificate cannot be verified or is damaged
Hot 100 depth first
Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“