当前位置:网站首页>1404. number of steps to reduce binary representation to 1
1404. number of steps to reduce binary representation to 1
2022-06-28 05:34:00 【AlbertOS】
introduce
Give you a number in binary form s . Please go back and reduce it to 1 Number of steps required :
- If the current number is even , Divide it by 2 .
- If the current number is odd , Then add 1 .
The title ensures that you can always change the test case into 1 .
Example
Input :s = “1101”
Output :6
explain :“1101” Represents a decimal number 13 .
Step 1) 13 Is odd , Add 1 obtain 14
Step 2) 14 It's even , except 2 obtain 7
Step 3) 7 Is odd , Add 1 obtain 8
Step 4) 8 It's even , except 2 obtain 4
Step 5) 4 It's even , except 2 obtain 2
Step 6) 2 It's even , except 2 obtain 1
Input :s = “10”
Output :1
explain :“10” Represents a decimal number 2 .
Step 1) 2 It's even , except 2 obtain 1
Input :s = “1”
Output :0
Answer key
There is a method called simulation , Directly simulate the practice in the topic , Count , To 1 Jump out of
If the current number is even , Divide it by 2 2 2. When s s s For binary representation , It is equivalent to removing the end of 0 0 0
If the current number is odd , Then add 1 1 1. When s s s For binary representation , It is equivalent to adding... To the last bit 1 1 1( Pay attention to carry requirements )
class Solution {
public:
int numSteps(string s) {
int steps = 0;
while (s != "1") {
// Count
++steps;
if (s.back() == '0') {
// Even numbers
s.pop_back();
}
else {
// First step : Find the lowest 0
// The second step : Put this 0 become 1, And all the following 1 become 0, That's it +1
// Specially , If s All of them 1, Then there will be extra carry
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] == '1') {
s[i] = '0';
if (i == 0) {
s = "1" + s;
break;
}
}
else {
s[i] = '1';
break;
}
}
}
}
return steps;
}
};
Another solution
- If the last one is 0( even numbers ), Then move directly to the right ( except 2)
- If the last one is 1( Odd number ), You need to add one , Reaction on a binary string is equivalent to constant carry , Take a few examples
11001 -> 11010 -> 1101
1011 -> 1100 -> 110 -> 11
As can be seen from the above example , A phased operation we can do is : Add 1 after , Will end with 0 All removed , The total number of steps required is : 1( carry ) + Continuous from the current bit 1 The number of ( It is equivalent to how many new... Are generated at the end of carry 0)
class Solution {
public:
int numSteps(string s) {
int idx = s.size()-1;
int ans = 0;
while(idx > 0)// The first place must be left at last 1, Not calculated separately
{
if(s[idx] == '0')
{
ans++;
idx--;
}
else
{
// carry +1
ans++;
while(idx >= 0 && s[idx] == '1')// After carry , Continuous carry ( There may be 1111->10000 The situation of )
{
ans++;
idx--;
}
if(idx > 0)
s[idx]='1';
}
}
return ans;
}
};
边栏推荐
- When excel copies the contents of a row, the columns are separated by the tab "\t"
- [JVM] - memory partition in JVM
- How to do a good job of dam safety monitoring
- msa. h: There is no such file or directory
- Determine whether an attribute exists in an object
- ? How to write the position to output true
- Study on modified triphosphate: lumiprobe amino-11-ddutp
- RL 实践(0)—— 及第平台辛丑年冬赛季【Rule-based policy】
- 密码学笔记
- 独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
猜你喜欢

Sqlmap tool user manual

Function reentry caused by Keil C51's data overlaying mechanism

小球弹弹乐

JSP connects with Oracle to realize login and registration (simple)

【无标题】drv8825步进电机驱动板子原理图

Wedding studio portal applet based on wechat applet

北斗三号短报文终端在大坝安全监测方案的应用

Shutter nestedscrollview sliding folding head pull-down refresh effect

codeforces每日5题(均1700)

Jdbc的使用
随机推荐
指定默认参数值 仍报错:error: the following arguments are required:
Can wechat applets import the data in the cloud development database into makers with one click in the map component
数据中台:一篇带你深入浅出了解数据中台
V4L2 驱动层分析
数据仓库:DWS层设计原则
Disable right-click, keyboard open console events
mysql 导出查询结果成 excel 文件
阴阳师页面
拉萨手风琴
[C language practice - printing hollow square and its deformation]
Concurrent wait/notify description
Shutter nestedscrollview sliding folding head pull-down refresh effect
2022 Western pastry (Advanced) test question simulation test platform operation
How to learn programmable logic controller (PLC)?
qtcanpool 知 07:Ribbon
Reactive dye research: lumiprobe af594 NHS ester, 5-isomer
What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
ERP软件公司选型的重要根据
Rxswift -- (1) create a project
Error: the following arguments are required: