当前位置:网站首页>June 9th training day - bit operation
June 9th training day - bit operation
2022-06-12 06:30:00 【Straying into the crowd】
List of articles
Preface
The content of today's algorithm is : An operation
One 、 Binary spacing
Given a positive integer n, Find and return to n In the binary representation of adjacent 1 Between Longest distance . If There are no two adjacent 1, return 0.
If only 0 Put two 1 Separate ( May not exist 0 ), We think these two 1 each other adjacent . Two 1 The distance between them is the absolute difference of their positions in the binary representation . for example ,“1001” Two of them 1 The distance to 3 .
One 、 Ideas :
(1) Use a variable to indicate whether it exists in binary 1 And store the previous 1, Use another variable to store the real-time location ( Since it is a binary number, there is no need to worry about whether there will be Yes 1 The previous case is zero , It will end the cycle directly )
(2) Make a circular judgment , If it conforms to 1, Do it once. 1 And 1 Judging the length between And Assignment of existing variables , If not, the length will be shifted to the right and the real-time variable will be increased ;
Two 、 Source code
class Solution {
public:
int binaryGap(int n) {
int maxd=0;
int pre=-1,now=0;
while(n){
if(n&1){
if(pre!=-1){
maxd=max(now-pre,maxd);
}
pre=now;
}
n>>=1;
++now;
}
return maxd;
}
};
// Hero's code
// I wanted to n To binary stored in the array for a double pointer judgment , Not very familiar hh
3、 ... and . Knowledge point
An operation & ,>>
Today is a contestant …
边栏推荐
- Bert use
- It only takes 10 minutes to understand the underlying principle of NiO
- SQL 注入读写文件
- Computer composition and design work06 - based on MIPS
- Nocturnal simulator ADB view log
- 六月集训 第二天——字符串
- LeetCode-1350. Invalid students
- Whether the modification of basic type and reference type is valid
- LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
- Remap function of C different interval mapping
猜你喜欢

It only takes 10 minutes to understand the underlying principle of NiO

Leetcode January 10 daily question 306 Additive number

. Net core - pass Net core will Net to cross platform

Bert Chinese classification model training + reasoning + deployment

MLP sensor

Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces

Information content security experiment of Harbin Institute of Technology

How to build your own website (using the pagoda panel)

Multithreading (4) -- no lock (2) -- Atomic related atomic classes

Bert use
随机推荐
GET 和 POST 的区别及留言板代码实现
Deep and detailed analysis of PHP one sentence Trojan horse
LeetCode-1716. Calculate the amount deducted from the bank
PHP read / write cookie
About why GPU early-z reduces overdraw
Cause analysis of motion blur / smear caused by camera shooting moving objects
Install MySQL tutorial
Single channel picture reading
CONDA create use virtual environment
Dlib face detection
JS variable scope
Explanation of sensor flicker/banding phenomenon
SQL 注入-盲注
Multithreading (V) -- concurrency tools (I) -- thread pool (II) -- related contents of ThreadPoolExecutor
Redis basic notes
RMB classification II
Chartextcnn (Ag dataset - news topic classification)
Codeforces Round #793 (Div. 2) A B C
Video based fire smoke detection using robust AdaBoost
Bert Chinese classification model training + reasoning + deployment