当前位置:网站首页>1404. 将二进制表示减到1的步骤数
1404. 将二进制表示减到1的步骤数
2022-06-28 05:28:00 【AlbertOS】
引入
给你一个以二进制形式表示的数字 s 。请你返回按下述规则将其减少到 1 所需要的步骤数:
- 如果当前数字为偶数,则将其除以 2 。
- 如果当前数字为奇数,则将其加上 1 。
题目保证你总是可以按上述规则将测试用例变为 1 。
示例
输入:s = “1101”
输出:6
解释:“1101” 表示十进制数 13 。
Step 1) 13 是奇数,加 1 得到 14
Step 2) 14 是偶数,除 2 得到 7
Step 3) 7 是奇数,加 1 得到 8
Step 4) 8 是偶数,除 2 得到 4
Step 5) 4 是偶数,除 2 得到 2
Step 6) 2 是偶数,除 2 得到 1
输入:s = “10”
输出:1
解释:“10” 表示十进制数 2 。
Step 1) 2 是偶数,除 2 得到 1
输入:s = “1”
输出:0
题解
有一种方法叫模拟,直接模拟题目中的做法,计数,到1的时候跳出
如果当前数字为偶数,则将其除以 2 2 2。当 s s s 为二进制表示时,就相当于去除末尾的 0 0 0
如果当前数字为奇数,则将其加上 1 1 1。当 s s s 为二进制表示时,就相当于对末位加上 1 1 1(注意进位要求)
class Solution {
public:
int numSteps(string s) {
int steps = 0;
while (s != "1") {
//计数
++steps;
if (s.back() == '0') {
// 偶数的情况
s.pop_back();
}
else {
// 第一步:找出最低位的 0
// 第二步:把这个 0 变成 1,并将后面所有的 1 变成 0,这样就实现了 +1
// 特别地,如果 s 中全是 1,那么会有额外的进位
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;
}
};
另一种解法
- 如果末位是0(偶数),则直接右移(除2)
- 如果末位是1(奇数),则需要加一,反应在二进制串上相当于不断进位,举几个例子
11001 -> 11010 -> 1101
1011 -> 1100 -> 110 -> 11
从以上例子可以看出,我们可以做的一个阶段性操作为:加1后,将末尾的0都去掉 ,总共需要的步骤数为: 1(进位) + 当前位起连续的1的个数(相当于进位后末尾新产生多少个0)
class Solution {
public:
int numSteps(string s) {
int idx = s.size()-1;
int ans = 0;
while(idx > 0)//第一位最后肯定剩1,不另计算
{
if(s[idx] == '0')
{
ans++;
idx--;
}
else
{
//进位+1
ans++;
while(idx >= 0 && s[idx] == '1')//进位后,连续进位(可能有1111->10000的情况)
{
ans++;
idx--;
}
if(idx > 0)
s[idx]='1';
}
}
return ans;
}
};
边栏推荐
- 拉萨手风琴
- Have you finished the examination of level II cost engineer? There are also qualification regulations!
- JS text box loses focus to modify width text and symbols
- Error: the following arguments are required:
- 2022 new version NFT source code source code of China meta universe digital collection art trading platform
- [Linux] - using xshell to install MySQL on Linux and realize the deployment of webapp
- Store inventory management system source code
- Binder面试之:内存管理单元
- Yunda's cloud based business in Taiwan construction 𞓜 practical school
- 数据仓库:金融/银行业主题层划分方案
猜你喜欢

Keil C51的Data Overlaying机制导致的函数重入问题

sqlmap工具使用手册

jq图片放大器

Create NFS based storageclass on kubernetes

Based on the order flow tool, what can we see?
![[Linux] - using xshell to install MySQL on Linux and realize the deployment of webapp](/img/07/e044a6ef14a6576dbee1c6a009ab4f.png)
[Linux] - using xshell to install MySQL on Linux and realize the deployment of webapp

2022 new version NFT source code source code of China meta universe digital collection art trading platform

Blog login box

When excel copies the contents of a row, the columns are separated by the tab "\t"

sklearn 特征工程(总结)
随机推荐
Study on modified triphosphate: lumiprobe amino-11-ddutp
jsp连接oracle实现登录注册(简单)
Wireless sensor network learning notes (I)
Hundreds of lines of code to implement a script interpreter
WordPress zibll sub theme 6.4.1 happy version is free of authorization
Blog login box
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
Latest Windows version 5.0.14 of redis
数据中台:AI中台的实施与总结
Office is being updated and the application cannot start normally
msa. h: There is no such file or directory
Disable right-click, keyboard open console events
Oracle 常用基础函数
独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
Programmer - Shepherd
JSP
線條動畫
Function reentry caused by Keil C51's data overlaying mechanism
Binder面试之:内存管理单元
Online yaml to JSON tool