当前位置:网站首页>LeetCode 1611. The minimum number of operations to make an integer 0
LeetCode 1611. The minimum number of operations to make an integer 0
2022-04-23 09:19:00 【Where do heroes come from】

List of articles
One 、 subject
1、 Title Description
Give you an integer n n n, You need to repeat the following several times to convert it to 0 0 0 :
1) Flip n n n The rightmost bit in the binary representation of ( The first 0 0 0 position ).
2) If the first ( i − 1 ) (i-1) (i−1) Position as 1 1 1 And from the ( i − 2 ) (i-2) (i−2) Position to the first 0 0 0 All for 0 0 0, Then flip n n n In the binary representation of i i i position .
Return to n n n Convert to 0 0 0 Minimum number of operations .
The sample input :n = 3
Sample output :2
2、 Basic framework
- C Language The basic framework code given in version is as follows :
int minimumOneBitOperations(int n){
}

3、 Original link
LeetCode 1611. Change an integer to 0 The minimum number of operations
Two 、 Problem solving report
1、 Thought analysis
( 1 ) (1) (1) 1 1 1 adopt 1 1 1 Time become 0 0 0;
( 2 ) (2) (2) 10 10 10 By first becoming 11 11 11, Then flip the highest position to 01 01 01, And then it becomes 0 0 0, in total 3 Time ;
( 3 ) (3) (3) 100 100 100 adopt 3 The next change is 110 110 110, Then flip the highest position to 010 010 010, Become... Again and again 0 0 0, in total 7 Time ;
( 4 ) (4) (4) According to mathematical induction , We found that , about 1 0...0 ⏟ k 1\underbrace{0...0}_{k} 1k
0...0, Change to 0 0 0 Times of 2 k + 1 − 1 2^{k+1} - 1 2k+1−1 Time ;
( 5 ) (5) (5) Then we compare a number n n n Binary decomposition n = ( 101011 ) 2 n = (101011)_2 n=(101011)2 , Then we must first find a way to put the highest 1 1 1 become 0 0 0, So it turns into seeking general ( 01011 ) 2 (01011)_2 (01011)2 Turn into ( 10000 ) 2 (10000)_2 (10000)2 The minimum number of steps ; And for ( 01011 ) 2 (01011)_2 (01011)2 To translate into ( 10000 ) 2 (10000)_2 (10000)2, It can be transformed into ( 1011 ) 2 (1011)_2 (1011)2 Turn it into ( 1000 ) 2 (1000)_2 (1000)2 The minimum number of steps ; Then is ( 011 ) 2 (011)_2 (011)2 To become ( 000 ) 2 (000)_2 (000)2, That is to say ( 11 ) 2 (11)_2 (11)2 become ( 00 ) 2 (00)_2 (00)2;
( 6 ) (6) (6) So we get the algorithm :
( 6.1 ) (6.1) (6.1) First of all, will n n n Binary decomposition ;
( 6.2 ) (6.2) (6.2) If you want to change a number to 1 0...0 ⏟ k 1\underbrace{0...0}_{k} 1k
0...0 , Call recursion dfs1(n); If you want to change to 0 0 0, Call recursion dfs2(n), So recursively solve dfs2(n) That's the answer .
2、 Time complexity
Worst time complexity O ( l o g 2 n ) O(log_2n) O(log2n) .
3、 Code details
int dfs1(int *bit, int d); // (1)
int dfs2(int *bit, int d); // (2)
int dfs1(int *bit, int d) {
if(d == 0) {
return bit[0] ^ 1;
}
if(!bit[d]) {
return dfs1(bit, d-1) + 1 + ( (1<<d) - 1 );
}
return dfs2(bit, d-1);
}
int dfs2(int *bit, int d) {
if(d == 0) {
return bit[0];
}
if(bit[d]) {
return dfs1(bit, d-1) + 1 + ( (1<<d) - 1 );
}
return dfs2(bit, d-1);
}
int minimumOneBitOperations(int n){
if(n == 0) {
return 0;
}
int stk[100], top = 0;
while(n) {
stk[top++] = n & 1;
n >>= 1;
}
return dfs2(stk, top-1);
}
- ( 1 ) (1) (1)
int dfs1(int *bit, int d)Express b i t [ d : 0 ] → 1 0...0 ⏟ d bit[d:0] \to 1\underbrace{0...0}_{d} bit[d:0]→1d 0...0 The minimum number of steps ; - ( 2 ) (2) (2)
int dfs2(int *bit, int d)Express b i t [ d : 0 ] → 0 0...0 ⏟ d bit[d:0] \to 0\underbrace{0...0}_{d} bit[d:0]→0d 0...0 The minimum number of steps ; - ( 3 ) (3) (3) For these two functions , Judge
bit[d]Whether this bit is consistent with the required bit , If it's not consistent , You need to use rule 2 to flip ; If always , Then continue to calculate b i t [ d − 1 : 0 ] → 0 0...0 ⏟ d − 1 bit[d-1:0] \to 0\underbrace{0...0}_{d-1} bit[d−1:0]→0d−1 0...0 The minimum number of steps ;
3、 ... and 、 Little knowledge of this topic
When solving a problem recursively , Abstract out the intermediate steps , It will be much clearer ;
Four 、 Instructions for group addition
I believe most of my articles are 「 College students' 」, Those who can go to college are 「 elite 」, Then we naturally want to 「 Keep improving 」, If you still 「 Freshman 」, So great , You have a lot of time , Of course you can choose 「 binge-watch 」, However ,「 Learn algorithm well 」, Three years later, you will naturally 「 Can't speak in the same day 」.
So here , I sorted out 「 Dozens of basic algorithms 」 The classification of , Click to open :
If the link is blocked , Or there is a permission problem , You can talk to the author in private to solve .

List of general problem sets :















To make it interesting , as well as 「 Take care of beginners 」, At present, the subject only opens the simplest algorithm 「 Enumeration series 」 ( Include : Linear enumeration 、 Double pointer 、 The prefix and 、 Binary enumeration 、 Three part enumeration ), When there is Half the members finished painting 「 Enumeration series 」 After all the questions , Will open the next chapter , When this set of questions is finished , You're still in the group , Then you will become 「 In the dead of night 」 The panel of experts A member of the .
Don't underestimate this panel of experts , After three years, , You will be someone else so far behind that one can only see the dust of the rider ahead The existence of . If you want to join , You can contact me , Considering that everyone is a student , No, 「 The main source of income 」, On your way to God ,「 Won't ask for anything 」.
Contact the author , Or scan the author's home page QR code and add groups , Join the list of questions
Let the world have no algorithm that is difficult to learn
C Language free animation tutorial , Punch in with me ! 《 In broad daylight C Language 》
Let you form the habit of brushing questions continuously for nine days 《 Nine day training 》
beginner C Summary of language real problems 🧡《C Introduction to language 100 example 》🧡
Group learning , Grow together 《 Algorithm zero basis 100 speak 》
Learn a data structure from a few dynamic diagrams 《 Figure out the data structure 》
Gold Classic graphic tutorial for competitors 《 In the dead of night 》
版权声明
本文为[Where do heroes come from]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230633004764.html
边栏推荐
- 小程序报错:Cannot read property 'currentTarget' of undefined
- 112. 路径总和
- Kettle experiment
- Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
- 数据清洗 ETL 工具Kettle的安装
- MySQL小练习(仅适合初学者,非初学者勿进)
- Go language self-study series | golang structure pointer
- 小女孩行走
- Applet error: cannot read property'currenttarget'of undefined
- Find the sum of simple types of matrices
猜你喜欢

NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’

Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files

npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’

小程序报错:Cannot read property 'currentTarget' of undefined

【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)

Detailed explanation of delete, truncate and drop principles in MySQL database

Arbre de dépendance de l'emballage des ressources

Distributed message oriented middleware framework selection - Digital Architecture Design (7)

Using JS to realize a thousandth bit

Flink SQL realizes the integration of stream and batch
随机推荐
考研线性代数常见概念、问题总结
Redis Desktop Manager for Mac
To remember the composition ~ the pre order traversal of binary tree
Node installation
tsdf +mvs
First principle mind map
Kettle experiment (III)
Applet error: cannot read property'currenttarget'of undefined
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
DMP engine work summary (2021, lightsaber)
valgrind和kcachegrind使用運行分析
RSA 加密解密签名验签
How to render web pages
Kettle experiment
Get trustedinstaller permission
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
How does kubernetes use harbor to pull private images
Employee probation application (Luzhou Laojiao)
OpenCV中的图像处理 —— 轮廓入门+轮廓特征
Using JS to realize a thousandth bit
