【LeetCode】1404. 将二进制表示减到 1 的步骤数 Number of Steps to Reduce a Number in Binary Representation to One
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/
题目描述
给你一个以二进制形式表示的数字 s 。请你返回按下述规则将其减少到 1 所需要的步骤数:
如果当前数字为偶数,则将其除以 2 。
如果当前数字为奇数,则将其加上 1 。
题目保证你总是可以按上述规则将测试用例变为 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
示例 2:
输入:s = "10"
输出:1
解释:"10" 表示十进制数 2 。
Step 1) 2 是偶数,除 2 得到 1
示例 3:
输入:s = "1"
输出:0
提示:
1 <= s.length <= 500s由字符'0'或'1'组成。s[0] == '1'
题目大意
二进制表示的数字,经过多少次变化之后能变成1.
解题方法
转成十进制模拟
第一想法是先转成十进制数字,然后进行模拟。但是看了字符串的长度范围是500,即这个数字估计有 2 ^ 500 那么大。很显然,不能用普通的数字类型进行保存。
但是,可以使用大数类型去做。比如 Python 默认就支持无限大的数字,那么在 Python 中是可以直接转成 十进制 进行模拟的。
在 Java 中,可以使用 BigInteger 类来做。
Python 代码如下。
class Solution:
def numSteps(self, s: str) -> int:
step = 0
s_int = int(s, 2)
while s_int != 1:
if s_int % 2 == 0:
s_int //= 2
else:
s_int += 1
step += 1
return step
修改二进制字符串
既然是个二进制字符串,那么可以从最后一位就知道当前的数字是偶数还是奇数。然后根据规则对这个二进制字符串进行操作就好了。类似于字符串表示的数字进行加法。
稍微有点难度的就是二进制进行加一的操作,需要一个进位 carry 表示是否有进位。carry 默认是 1,表示要对当前的二进制字符串加 1.
C++代码如下:
class Solution {
public:
int numSteps(string s) {
int step = 0;
while (s != "1") {
int len = s.size();
if (s[len - 1] == '1') {
addOne(s);
} else {
s = s.substr(0, len - 1);
}
step ++;
}
return step;
}
void addOne(string& s) {
int N = s.size();
int carry = 1;
for (int i = N - 1; i >= 0; --i) {
if (s[i] == '1') {
if (carry == 1) {
s[i] = '0';
carry = 1;
}
} else {
if (carry == 1) {
s[i] = '1';
carry = 0;
}
}
}
if (carry == 1) {
s = "1" + s;
}
}
};
欢迎关注负雪明烛的刷题博客,leetcode刷题800多,每道都讲解了详细写法!
日期
2020 年 4 月 5 日 —— 好久不打周赛了
【LeetCode】1404. 将二进制表示减到 1 的步骤数 Number of Steps to Reduce a Number in Binary Representation to One的更多相关文章
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
- LeetCode:乘法表中的第K小的数【668】
LeetCode:乘法表中的第K小的数[668] 题目描述 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要 ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
随机推荐
- spring通过注解注册bean的方式+spring生命周期
spring容器通过注解注册bean的方式 @ComponentScan + 组件标注注解 (@Component/@Service...) @ComponentScan(value = " ...
- 全基因组选择育种(GS)简介
全基因组选择(Genomic selection, GS)是一种利用覆盖全基因组的高密度标记进行选择育种的新方法,可通过早期选择缩短世代间隔,提高育种值(Genomic Estimated Breed ...
- fastboot烧写Andriod 以及SD 卡烧写LinuxQT,
EMMC是一种FLASH,SD(TF)卡是另外的一种存储,通过控制拨码开关指引CPU去读EMMC还是SD卡的u-boot文件. u-boot的作用 初始化内存控制区,访问存储器,把内核从存储器读取出来 ...
- LightningChart JS v.3.3.0全新版本现已发布!
LightningChart JS v.3.3.0已经发布啦!!! 欢迎了解更多关于最新的性能改进.新的用户界面功能和新的图表类型的信息! WebGL兼容性和新的UI功能 WebGL是Lightnin ...
- 浅谈MySQL数据库面试必要掌握知识点
概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 MySQL官方地址 https://www.mysql.com/ MySQL 8系列最新版本为8.0.27,5系列的最 ...
- 最长公共子序列问题(LCS) 洛谷 P1439
题目:P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 关于LCS问题,可以通过离散化转换为LIS问题,于是就可以使用STL二分的方法O(nlogn ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- 【AWS】【Basis】基础概念
1.基础服务类型: 1.1. 链接: 官方文档,很详细:https://www.amazonaws.cn/products/#compute_networking/?nc1=f_dr 这个是一个whi ...
- Linux:变量$#,$@,$0,$1,$2,$*,$$,$?
写一个简单的脚本 vim var 脚本内容如下: #!/bin/sh echo "the number of parameters passed to the script: $#" ...
- 【C/C++】编码(腾讯)
假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, - -, b, ba, ...