当前位置:网站首页>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;
}
}边栏推荐
- 汉诺塔问题的求解与分析
- C Gaode map obtains the address according to longitude and latitude
- gocv图片读取并展示
- D interface and domain problems
- Getting started with k8s: building MySQL with Helm
- zipkin 简单使用
- Openshift container platform community okd 4.10.0 deployment
- C call system sound beep~
- 一个经典约瑟夫问题的分析与解答
- Googlenet network explanation and model building
猜你喜欢

Sqli labs (post type injection)

汉诺塔问题的求解与分析

KubeSphere 虚拟化 KSV 安装体验

C language replaces spaces in strings with%20

Linux安装Oracle Database 19c

Sqli labs level 1

Minecraft module service opening

ARP及ARP欺骗

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away

Detailed explanation of NIN network
随机推荐
Pointer initialization
Minecraft空岛服开服
一个经典约瑟夫问题的分析与解答
Installing Oracle database 19C RAC on Linux
Web security -- core defense mechanism
Openshift build image
zipkin 简单使用
Installing Oracle database 19C for Linux
C Gaode map obtains the address according to longitude and latitude
HCIA—数据链路层
What is SQL injection
Web security -- Logical ultra vires
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
HCIA - data link layer
Zipkin is easy to use
gocv图片裁剪并展示
Nacos 下载启动、配置 MySQL 数据库
Sqli labs (post type injection)
First week of JS study
Introduction to the basic concept of queue and typical application examples