当前位置:网站首页>Practice with the topic of bit operation force deduction
Practice with the topic of bit operation force deduction
2022-06-26 14:13:00 【hedgehog:】
Basic knowledge of bit operation
An operation (&、|、^、~、>>、 | Novice tutorial (runoob.com)
https://www.runoob.com/w3cnote/bit-operation.html Excerpt power button topic 136 、137、 260 、645
subject 1: 136. A number that appears only once
https://leetcode-cn.com/problems/single-number/

Code :
class Solution {
public int singleNumber(int[] nums) {
int sn=0;
// According to the nature of the xor - reflexivity The other elements appear twice So all XORs are OK
for(int i=0;i<nums.length;i++){
sn^=nums[i];
}
return sn;
}
}
result :

subject 2:137. A number that appears only once II
https://leetcode-cn.com/problems/single-number-ii/

Code :
class Solution {
public int singleNumber(int[] nums) {
int res=0;
for(int i=0;i<32;i++){
// Traverse all bits from low to high
int sum=0;
// Traverse all the numbers in the array
for(int num:nums){
// Sum the current bit & Take a bit
//sum+=(num&(1<<i)); That's not good Still thinking about why ...
sum+=(num>>i)&1);
}
if(sum%3==1){
// If the sum of this bit is modulo three Have remainder Then assign the remainder to the result value
//| Make a position 1 because For this problem, the single number with remainder This one knows So is 1 了
res|=(1<<i);
}
}
return res;
}
}result :
subject 3:260. A number that appears only once III
https://leetcode-cn.com/problems/single-number-iii/
Code :
class Solution {
public int[] singleNumber(int[] nums) {
int temp=0;
int flag=0;
int sn1=0;
int sn2=0;
// All values XOR Get the XOR value of two numbers that occur only once
for(int num:nums){
temp^=num;
}
// Traversal XOR results Check which of the two numbers is different
for(int i=0;i<32;i++){
// This one The two numbers are different
if(((temp>>i)&1)==1){
flag=i;
break;
}
}
// There are two groups of XOR
for(int num:nums){
if(((num>>flag)&1)==0){
sn1^=num;
}else{
sn2^=num;
}
}
return new int[]{sn1,sn2};
}
}result :

subject 4:645. The wrong set
https://leetcode-cn.com/problems/set-mismatch/

Code :
class Solution {
public int[] findErrorNums(int[] nums) {
int temp=0;
int flag=1;
int sn1=0;
int sn2=0;
// All values and 1-n XOR together The repeated and the lost are singular
// Get duplicate and lost The XOR value of two numbers
for(int i=1;i<=nums.length;i++){
temp^=nums[i-1];
temp^=i;
}
// Traversal XOR results Check which of the two numbers is different
while((temp&flag)==0)
flag<<=1;
// There are two groups of XOR Get the missing number and the repetition number
for(int i=1;i<=nums.length;i++){
if((nums[i-1]&flag)==0)
sn1^=nums[i-1];
else
sn2^=nums[i-1];
if((i&flag)==0)
sn1^=i;
else
sn2^=i;
}
// Determine which number is repeated
for(int num:nums){
if(sn1==num)
return new int[]{sn1,sn2};
}
return new int[]{sn2,sn1};
}
}result :
Reference resources :
边栏推荐
- C | analysis of malloc implementation
- BP neural network for prediction
- 免费的机器学习数据集网站(6300+数据集)
- 33. Use rgbd camera for target detection and depth information output
- [MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
- Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
- 9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
- Hard (magnetic) disk (II)
- Correlation analysis related knowledge
- Taishan Office Technology Lecture: four cases of using bold font
猜你喜欢

Codeforces Global Round 21A~D

爱可可AI前沿推介(6.26)

Common operation and Principle Exploration of stream

What is the use of index aliases in ES

ICML 2022 | limo: a new method for rapid generation of targeted molecules

8.Ribbon负载均衡服务调用

RISC-V 芯片架构新规范

Wechat applet -picker component is repackaged and the disabled attribute is added -- below

永远不要使用Redis过期监听实现定时任务!

基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
随机推荐
[wc2006] director of water management
Correlation analysis related knowledge
Exercises under insect STL string
Range of types
array
C language | Consortium
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
GEE——全球人类居住区网格数据 1975-1990-2000-2014
windows版MySQL软件的安装与卸载
Relevant knowledge of information entropy
C language | the difference between heap and stack
Pointer
Build your own PE manually from winpe of ADK
Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
8. Ribbon load balancing service call
Lucky numbers in the matrix
[cqoi2015] task query system
33. Use rgbd camera for target detection and depth information output
Gartner 2022年顶级战略技术趋势报告
虫子 类和对象 上