当前位置:网站首页>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
边栏推荐
- [58] length of the last word [leetcode]
- Group Backpack
- [SQL Server fast track] view and cursor of database
- Using JS to realize a thousandth bit
- 员工试用期转正申请书(泸州老窖)
- BK3633 规格书
- SQL used query statements
- Technological innovation in government affairs in the construction of Digital Government
- 112. Path sum
- [reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
猜你喜欢

112. 路径总和

Machine learning (VI) -- Bayesian classifier

112. Path sum

653. Sum of two IV - input BST
![[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)](/img/a2/b50fadad859a050eecfa15a436e126.png)
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)

Number of islands

LeetCode_DFS_中等_1254. 统计封闭岛屿的数目

Resource packaging dependency tree

MySQL小練習(僅適合初學者,非初學者勿進)

The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
随机推荐
Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
The most concerned occupations after 00: civil servants ranked second. What was the first?
108. 将有序数组转换为二叉搜索树
[original] use system Text. JSON formats the JSON string
Machine learning (VI) -- Bayesian classifier
DMP engine work summary (2021, lightsaber)
AQS & reentrantlock implementation principle
On array replication
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
About CIN, scanf and getline, getchar, CIN Mixed use of getline
653. 两数之和 IV - 输入 BST
数据清洗 ETL 工具Kettle的安装
Data visualization: use Excel to make radar chart
Go language self-study series | golang method
Using JS to realize a thousandth bit
資源打包關系依賴樹
Go language self-study series | golang nested structure
成功的DevOps Leader 应该清楚的3个挑战
【原创】使用System.Text.Json对Json字符串进行格式化
[boutique] using dynamic agent to realize unified transaction management II
