当前位置:网站首页>Leetcode sword finger offer brush questions - day 22
Leetcode sword finger offer brush questions - day 22
2022-07-02 08:53:00 【DEGv587】
Leetcode The finger of the sword Offer How to brush questions :
The finger of the sword Offer 56 - I. The number of occurrences of numbers in an array
solution : Group XOR
class Solution {
public int[] singleNumbers(int[] nums) {
// Traverse nums Execute XOR
int n = 0, m = 1, x = 0, y = 0;
for (int num : nums) {
n ^= num;
}
// here n = x ^ y
// Cyclic shift to the left , Calculation m
while ((n & m) == 0) {
m <<= 1;
}
// Traverse nums grouping
for (int num : nums) {
if ((num & m) != 0) {
// When num & m != 0
x ^= num;
} else {
// When num & m == 0
y ^= num;
}
}
// Returns the number that appears once
return new int[]{x, y};
}
}
The finger of the sword Offer 56 - II. The number of occurrences of numbers in an array II
Solution 1 :hashmap
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
for (int n : nums) {
if (map.get(n) == 1) {
return n;
}
}
return -1;
}
}
Solution 2 : An operation
If a number appears 3 Time , Every bit of its binary also appears 3 Time .
If you add up each bit of the binary representation of all the numbers that appear three times , Then everyone can be 3 to be divisible by .
We add up every bit of the binary representation of all the numbers in the array .
If someone can be 3 to be divisible by , Then this one's of the number that appears only once must be 0.
If someone can't be 3 to be divisible by , Then the position of the number that appears only once must be 1.
class Solution {
public int singleNumber(int[] nums) {
int[] k = new int[32];
for (int n : nums) {
for (int i = 0; i < 32; ++i) {
// Cycle every number of hours , Add up each bit of the binary representation of all the numbers in the array separately
k[i] += n & 1;
n >>= 1;
}
}
int ret = 0;
for (int i = 31; i >= 0; --i) {
ret <<= 1;
if (k[i] % 3 == 1) {
// If someone can't be 3 to be divisible by , that ret This bit must be 1
ret |= 1;// or ( Yes 1 be 1) Give the location 1
}
// If it can be 3 to be divisible by , So this one is right ret It must be 0
}
return ret;
}
}
边栏推荐
- 整理秒杀系统的面试必备!!!
- Move a string of numbers backward in sequence
- Find the node with the smallest value range in the linked list and move it to the front of the linked list
- Use Wireshark to grab TCP three handshakes
- History of Web Technology
- HCIA—数据链路层
- Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
- Call Stack
- Minecraft模组服开服
- gocv拆分颜色通道
猜你喜欢
OpenShift 容器平台社区版 OKD 4.10.0部署
Service de groupe minecraft
Application of kotlin - higher order function
ARP及ARP欺骗
[untitled]
What is SQL injection
Linux binary installation Oracle database 19C
Introduction to the basic concept of queue and typical application examples
Minecraft group service opening
Solid principle: explanation and examples
随机推荐
Connect function and disconnect function of QT
Sqli labs level 12
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
Flex layout
KubeSphere 虚拟化 KSV 安装体验
Network security - summary and thinking of easy-to-use fuzzy tester
Use Wireshark to grab TCP three handshakes
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
图像变换,转置
Synchronize files using unison
k8s入门:Helm 构建 MySQL
Tcp/ip - transport layer
Programming ape learning English - imperative programming
zipkin 简单使用
Minecraft安装资源包
STM32 new project (refer to punctual atom)
gocv拆分颜色通道
Oracle 相关统计
Solution and analysis of Hanoi Tower problem
[flask] ORM one-to-one relationship