news 2026/5/13 19:25:18

LeetCode 1404.将二进制表示减到 1 的步骤数:模拟+高精度模拟玩玩(运算符重载)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LeetCode 1404.将二进制表示减到 1 的步骤数:模拟+高精度模拟玩玩(运算符重载)

【LetMeFly】1404.将二进制表示减到 1 的步骤数:模拟+高精度模拟玩玩(运算符重载)

力扣题目链接:https://leetcode.cn/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 <= 500
  • s由字符'0''1'组成。
  • s[0] == '1'

解题方法:模拟

Python直接转int,C++使用字符串双端队列代替

  • 时间复杂度O ( l e n ( s ) ) O(len(s))O(len(s))
  • 空间复杂度O ( log ⁡ s ) O(\log s)O(logs)O ( l e n ( s ) ) O(len(s))O(len(s))

AC代码

C++
/* * @LastEditTime: 2026-02-27 22:29:56 */classLetsNum{private:// 低位->高位deque<char>num;public:// From binary stringLetsNum(string&s){for(charc:s){*this<<=1;*this+=c;}}LetsNum&operator++(int){intcarry=1;for(deque<char>::iterator it=num.begin();it!=num.end();it++){carry+=*it-'0';*it=carry%10+'0';carry/=10;}if(carry){num.push_back(carry+'0');}return*this;}// only 0/1 supportedLetsNum&operator+=(constint&n){if(!n){return*this;}(*this)++;return*this;}// only 0/1 supportedLetsNum&operator+=(constchar&c){*this+=c-'0';return*this;}// only <<1 supportedLetsNum&operator<<=(constint&){intcarry=0;for(deque<char>::iterator it=num.begin();it!=num.end();it++){carry+=(*it-'0')*2;*it=carry%10+'0';carry/=10;}if(carry){num.push_back(carry+'0');}return*this;}// only >>1 supportedLetsNum&operator>>=(constint&){intmod=0;for(deque<char>::reverse_iterator it=num.rbegin();it!=num.rend();it++){mod=mod*10+(*it-'0');*it=mod/2+'0';mod%=2;}if(*num.rbegin()=='0'){num.pop_back();}return*this;}// only 1 supportedbooloperator!=(constint&){returnnum.size()!=1||*num.begin()!='1';}boolisOdd(){return(*num.begin()-'0')%2;}voidprint(){for(deque<char>::reverse_iterator it=num.rbegin();it!=num.rend();it++){putchar(*it);}puts("");}};classSolution{public:intnumSteps(string s){LetsNumnum(s);intans=0;while(num!=1){if(num.isOdd()){num+=1;}else{num>>=1;}ans++;}returnans;}};#ifdefined(_WIN32)||defined(__APPLE__)/* 1101 */intmain(){string s;while(cin>>s){Solution sol;cout<<sol.numSteps(s)<<endl;}return0;}#endif
Python
''' LastEditTime: 2026-02-26 23:59:25 '''""" 1101 1110 111 1000 100 10 1 """classSolution:defnumSteps(self,s:str)->int:a=int(s,2)ans=0whilea!=1:ifa%2:a+=1else:a//=2ans+=1# print(a)returnans# print(Solution.numSteps('', '1101'))

C++完胜!(bushi)

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 20:30:26

DeepSeek总结PostgreSQL中统计信息对查询性能的影响

PostgreSQL中统计信息对查询性能的影响 原文地址&#xff1a;https://boringsql.com/posts/postgresql-statistics/ 一、为什么查询会变慢&#xff1f; 查询性能取决于执行计划&#xff0c;而执行计划的质量依赖于统计信息的准确性。当统计信息过时&#xff08;如批量数据加载、…

作者头像 李华
网站建设 2026/4/18 20:30:26

在仅有runtime的Docker镜像中使用dotnet global tool的方法

Docker镜像中使用dotnet global tool△ 背景和问题概述.NET Core 自2.1版本起便支持了Global tool特性。通过这一特性&#xff0c;用户能够借助命令行轻松实现众多功能&#xff0c;而微软提供的dotnet诊断工具系列也均以global tool形式呈现&#xff0c;使得使用过程更为便捷。…

作者头像 李华
网站建设 2026/4/18 20:30:37

卢森堡大学突破:AI实现人类式持续学习防遗忘

当你学会骑自行车后&#xff0c;即使多年不骑也不会忘记&#xff0c;但同时你还能继续学习开汽车。然而&#xff0c;对于人工智能来说&#xff0c;学习新技能往往意味着忘记旧技能&#xff0c;就像一个只有一间房的仓库&#xff0c;每次放入新东西就必须丢掉旧的。这种现象被称…

作者头像 李华
网站建设 2026/4/18 20:30:37

【2025最新】基于SpringBoot+Vue的HTML语言环保网站管理系统源码+MyBatis+MySQL

摘要 随着全球环境问题的日益严峻&#xff0c;环保意识的提升成为社会发展的关键议题。互联网技术的快速发展为环保信息的传播和管理提供了高效便捷的途径。环保网站管理系统作为信息传播的重要载体&#xff0c;能够整合环保新闻、政策法规、公益活动等内容&#xff0c;帮助公众…

作者头像 李华