当前位置:网站首页>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;
}
}
边栏推荐
- 【FreeSwitch开发实践】专栏简介
- Basic exercises of C language for beginners
- [QNX Hypervisor 2.2用户手册]9.1 配置变量
- Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
- [data mining engineer - written examination] Haier company in 2022
- 【LeetCode第 83 场双周赛】
- Hot 100 depth first
- Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
- About redis: there is still a risk of data loss after redis sets data persistence
- For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser
猜你喜欢

深入理解协程

Static extension configuration

The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘

Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN

VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network

Small farmers also have big goals in the test, and the latest big bat interview summary (constantly updating...)

Image processing: Generation 3 × Window of 3

Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
随机推荐
SQL CASE 多条件用法
Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
Concurrent programming 1-2
Thread pool summary
[the 83rd fortnight of leetcode]
VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network
Hypothesis test of Pearson correlation coefficient
Establishment of static route
Off screen rendering & FBO
Prometheus+node exporter+grafana monitoring server system resources
The postman test interface has 404 or 500 errors when the URL is configured correctly
落枕如何快速缓解
Tutorial on the principle and application of database system (049) -- MySQL query (XI): sub query
Create a self signed certificate to digitally sign exe files
An article teaches you the basic use of kubernetes
[QNX Hypervisor 2.2用户手册]9.1 配置变量
JS related knowledge
【FreeSwitch开发实践】专栏简介
Tutorial on principles and applications of database system (041) -- MySQL query (III): setting query conditions
Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi