【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 ...
随机推荐
- distmat 计算遗传距离
distmat 可用于计算遗传距离,得到距离矩阵 1 在线运算 可通过在线进行遗传距离的计算,网址:http://www.bioinformatics.nl/cgi-bin/emboss/distma ...
- 日常Java 2021/10/26
HashSet基于HashMap来实现的,是一个不允许有重复元素的集合.HashSet 允许有null 值. HashSet是无序的,即不会记录插入的顺序. HashSet不是线程安全的,如果多个线程 ...
- 日常Java 2021/10/24
Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...
- [源码解析] PyTorch分布式优化器(2)----数据并行优化器
[源码解析] PyTorch分布式优化器(2)----数据并行优化器 目录 [源码解析] PyTorch分布式优化器(2)----数据并行优化器 0x00 摘要 0x01 前文回顾 0x02 DP 之 ...
- 大数据学习day23-----spark06--------1. Spark执行流程(知识补充:RDD的依赖关系)2. Repartition和coalesce算子的区别 3.触发多次actions时,速度不一样 4. RDD的深入理解(错误例子,RDD数据是如何获取的)5 购物的相关计算
1. Spark执行流程 知识补充:RDD的依赖关系 RDD的依赖关系分为两类:窄依赖(Narrow Dependency)和宽依赖(Shuffle Dependency) (1)窄依赖 窄依赖指的是 ...
- jQuery无限载入瀑布流 【转载】
转载至 http://wuyuans.com/2013/08/jquery-masonry-infinite-scroll/ jQuery无限载入瀑布流 好久没更新日志了,一来我比较懒,二来最近也比较 ...
- [学习总结]3、Android---Scroller类(左右滑动效果常用的类)
参考资料:http://blog.csdn.net/vipzjyno1/article/details/24592591 非常感谢这个兄弟! 在android学习中,动作交互是软件中重要的一部分,其中 ...
- When do we pass arguments by reference or pointer?
在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...
- struct vs class in C++
在C++中,除了以下几点外,struct和class是相同的. (1)class的成员的默认访问控制是private,而struct的成员的默认访问权限是public. 例如,program 1会编译 ...
- shell 截取字符串实例教程
本节内容:shell字符串截取方法 1,去掉字符串最左边的字符 [root@jbxue ~]$ vi test.sh 1 STR="abcd" 2 STR=${STR#" ...