当前位置:网站首页>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 :
边栏推荐
- Exercises under insect STL string
- Zero basics of C language lesson 7: break & continue
- Taishan Office Technology Lecture: four cases of using bold font
- "Scoi2016" delicious problem solution
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
- 虫子 类和对象 中
- It is better and safer to choose which securities company to open an account for flush stock
- [sdoi2013] forest
- Zero basics of C language lesson 8: Functions
- d的is表达式
猜你喜欢

Create your own cross domain proxy server

Jenkins build prompt error: eacces: permission denied

9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》

Generation and rendering of VTK cylinder

Correlation analysis related knowledge

Freefilesync folder comparison and synchronization software

Wechat applet Registration Guide

C language | file operation and error prone points

Never use redis expired monitoring to implement scheduled tasks!

Included angle of 3D vector
随机推荐
Hands on data analysis unit 3 model building and evaluation
DOS command
Insect operator overloaded a fun
Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
CF676C Vasya and String
2021-10-18 character array
character constants
ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
Niuke challenge 53:c. strange magic array
Global variable vs local variable
Wechat applet Registration Guide
Memory considerations under bug memory management
Basic type of typescript
A must for programmers, an artifact utools that can improve your work efficiency n times
Matlab programming related knowledge
微信小程序注册指引
d检查类型是指针
Codeforces Global Round 21A~D
8. Ribbon load balancing service call