【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 ...
随机推荐
- Python 包管理工具 pip 与 conda
简介 pip是接触 python 后最早认识的包管理工具.通过使用 pip 能够自动下载和解决不同 python 模块的依赖问题,使 python 的配置过程变得简单. 与 pip 类似,conda ...
- [R]在dplyr基础上编写函数-(1)eval
tidyverse系列的R包虽然解放了大家的双手,但同时也束缚了我们重新编写函数的能力.在这一套语法中,要实现作为函数参数的字符串和变量之间的相互转换困难重重,但只要掌握了其中原理后,也就能够游刃有余 ...
- java数组中Arrays类
使用Arrays类之后要先导入包,即在开头添加这行: import.java.util.Arrays 1,排序:Arrays.sort(数组名) 排序后为数组升序. 2,将数组转换成字符串:Array ...
- error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64
今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...
- KEGG通路图应该怎么看(转载)
转载:http://www.omicshare.com/forum/thread-107-1-3219.html (出处: OmicShare Forum) 不管是RNA-seq的分析数据,还是蛋白组 ...
- 学习java 7.17
学习内容: 计算机网络 网络编程 网络编程三要素 IP地址 端口 协议 两类IP地址 IP常用命令: ipconfig 查看本机IP地址 ping IP地址 检查网络是否连通 特殊IP地址: 127. ...
- 学习java 7.14
学习内容: 标准输入输出流 输出语言的本质:是一个标准的输出流 字节打印流 字符打印流 对象序列化流 明天内容: 进程和线程 遇到问题: 用对象序列化流序列化一个对象后,假如我们修改了对象所属的类文件 ...
- Scala(四)【集合基础入门】
目录 一.Array 二. List 三.Set 四.Tuple 五.Map 一.Array package com.bigdata.scala.day01 /** * @description: 不 ...
- 【swift】长按事件绑定,平移滑动事件+坐标获取
为何把这两个事件归类在一起? 我后来才明白,iOS有一个手势事件(UiGestureRecognizer) 事件里有7个功能,不过我只试过前两个,也就是标题的这两个(长按.平移滑动) UILongPr ...
- 利用extern共享全局变量
方法: 在xxx.h中利用extern关键字声明全局变量 extern int a; 在xxx.cpp中#include<xxx.h> 再定义 int a; 赋不赋初值无所谓,之后该全局变 ...