当前位置:网站首页>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;
}
}
边栏推荐
- ARP及ARP欺骗
- Dip1000 implicitly tagged with fields
- Kubesphere virtualization KSV installation experience
- Luogu greedy part of the backpack line segment covers the queue to receive water
- Kubernetes deploys Loki logging system
- Analysis and solution of a classical Joseph problem
- Minecraft group service opening
- 使用递归函数求解字符串的逆置问题
- TCP/IP—传输层
- Linux安装Oracle Database 19c
猜你喜欢
Gateway 简单使用
Routing foundation - dynamic routing
Web技术发展史
OpenShift 部署应用
将一串数字顺序后移
IP protocol and IP address
Nacos 下载启动、配置 MySQL 数据库
Luogu greedy part of the backpack line segment covers the queue to receive water
Solution of Xiaomi TV's inability to access computer shared files
[blackmail virus data recovery] suffix Crylock blackmail virus
随机推荐
[blackmail virus data recovery] suffix Rook3 blackmail virus
Nacos download, start and configure MySQL database
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Mutex
Minecraft group service opening
Minecraft群組服開服
MYSQL安装出现问题(The service already exists)
Solid principle: explanation and examples
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
C# 将网页保存为图片(利用WebBrowser)
sqli-labs第1关
一个经典约瑟夫问题的分析与解答
What is SQL injection
[blackmail virus data recovery] suffix Crylock blackmail virus
ARP and ARP Spoofing
Application of kotlin - higher order function
[blackmail virus data recovery] suffix Hydra blackmail virus
Analysis and solution of a classical Joseph problem
[flask] ORM one-to-one relationship