当前位置:网站首页>Day260: the number III that appears only once
Day260: the number III that appears only once
2022-06-23 01:22:00 【Shallow look】
problem :
Given an array of integers nums, There are exactly two elements that only appear once , All the other elements appear twice . Find the two elements that only appear once . You can press In any order Return to the answer .
Advanced : Your algorithm should have linear time complexity . Can you just use constant space complexity to achieve ?
Example 1:
Input :nums = [1,2,1,3,2,5]
Output :[3,5] explain :[5, 3] It's also a valid answer .
Example 2:
Input :nums = [-1,0]
Output :[-1,0]
Example 3:
Input :nums = [0,1]
Output :[1,0]
source : Power button (LeetCode)
Answer key
Train of thought : Hashtable
Use the hash table to store the number of all elements in the integer array
The extracted number is 1 The elements of
class Solution {
public int[] singleNumber(int[] nums) {
Map<Integer, Integer> nums_count = new HashMap<>();
for(int i : nums)
nums_count.put(i,nums_count.getOrDefault(i,0)+1);
int[] a = new int[2];
int idx = 0;
for(int i : nums){
if(nums_count.get(i)==1)
a[idx++] = i;
}
return a;
}
}
Train of thought two : Exclusive or
It is known that :0 The result of exclusive or with an integer is the integer itself , The XOR result of two identical numbers is 0.
- Loop exclusive or to the integer array element , Get the XOR result that only two elements appear once sum;
- Optional sum Binary numbers are 1 Number of digits k,sum In Chinese, it means 1 Indicates that the value of the bit on this bit where the element only appears once is different , To distinguish two elements that only appear once ;
- Pass the first k Different bits , Divide the array elements into two parts ( Each section contains only one single element ), Redo XOR operation , Get two elements that only appear once a[0],a[1].
class Solution {
public int[] singleNumber(int[] nums) {
int sum = 0;
for(int i : nums) sum ^= i;
int k = -1;
for(int i = Integer.toBinaryString(sum).length(); i>0; i--){
if(((sum >> i) & 1) == 1) k = i;
}
int[] a = new int[2];
for(int i : nums){
if((( i >> k ) & 1) ==1) a[0] ^= i;
else a[1] ^= i;
}
return a;
}
}
边栏推荐
- Is it safe to open a new bond? How
- 3D打印微组织
- Vector 2 (friend and copy construction)
- Time complexity
- Tidb monitoring upgrade: a long way to solve panic
- Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
- Real topic of the 2020 Landbridge cup provincial competition - go square (dp/dfs)
- 一文读懂基于Redis的Amazon MemoryDB数据库
- LeetCode 206. 反转链表(迭代+递归)
- SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
猜你喜欢

A hundred lines of code to realize reliable delay queue based on redis

Dig three feet to solve the data consistency problem between redis and MySQL

Quelle est la structure et la façon dont les données sont stockées dans la base de données?

Charles garbled code problem solving

OSPF experiment in mGRE environment

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

three. JS simulated driving tour art exhibition hall - creating super camera controller

E-R图

Installation record of ros1noetic in Win 11

數據庫中數據的儲存結構和方式是什麼?
随机推荐
E-R diagram
cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧
OOP multiple storage (class template)
E-R图
Vector 1 (classes and objects)
OSPF experiment in mGRE environment
Node fetch download file
LINQ query
New progress in the construction of meituan's Flink based real-time data warehouse platform
Pat class A - 1012 the best rank (PIT)
How to set the power-off auto start of easycvr hardware box
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
Shell 日志与打印输出
总体标准差和样本标准差
Thead safety experience
Const defined variables and for of and for in in JS
Shell view help
B tree and b+ tree
How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
Cadence spb17.4 - Chinese UI settings