当前位置:网站首页>力扣6_1342. 将数字变成 0 的操作次数
力扣6_1342. 将数字变成 0 的操作次数
2022-07-08 00:39:00 【上课不要睡觉了】
给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。
示例 1:
输入:num = 14
输出:6
解释:
步骤 1) 14 是偶数,除以 2 得到 7 。
步骤 2) 7 是奇数,减 1 得到 6 。
步骤 3) 6 是偶数,除以 2 得到 3 。
步骤 4) 3 是奇数,减 1 得到 2 。
步骤 5) 2 是偶数,除以 2 得到 1 。
步骤 6) 1 是奇数,减 1 得到 0 。
示例 2:
输入:num = 8
输出:4
解释:
步骤 1) 8 是偶数,除以 2 得到 4 。
步骤 2) 4 是偶数,除以 2 得到 2 。
步骤 3) 2 是偶数,除以 2 得到 1 。
步骤 4) 1 是奇数,减 1 得到 0 。
示例 3:
输入:num = 123
输出:12
来源:力扣(LeetCode)
两种解法使用的原理均是根据二进制数的特性,根据所在位的0或1的情况确定对应的步骤数。
Java解法
class Solution {
public int numberOfSteps(int num) {
int ret = 0;
while (num > 0) {
ret += (num > 1 ? 1 : 0) + (num & 0x01);
//将&符号两边的数字转化成二进制,进行按位与运算
num >>= 1;
}
return ret;
}
}
Python解法
class Solution:
def numberOfSteps(self, num: int) -> int:
ans = 0
while num:
ans += num & 1
if num > 1:
ans += 1
num >>= 1
return ans
边栏推荐
- Flutter 3.0框架下的小程序运行
- 由排行榜实时更新想到的数状数值
- 神经网络与深度学习-5- 感知机-PyTorch
- cv2-drawline
- How to make the conductive slip ring signal better
- 快手小程序担保支付php源码封装
- Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
- Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
- PB9.0 insert OLE control error repair tool
- [error] error loading H5 weight attributeerror: 'STR' object has no attribute 'decode‘
猜你喜欢

Why does the updated DNS record not take effect?

Remote sensing contribution experience sharing

adb工具介绍

如何制作企业招聘二维码?

Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)

快手小程序担保支付php源码封装

Sum of submatrix

metasploit

Give some suggestions to friends who are just getting started or preparing to change careers as network engineers

Exit of processes and threads
随机推荐
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
谈谈 SAP 系统的权限管控和事务记录功能的实现
数据链路层及网络层协议要点
【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘
关于TXE和TC标志位的小知识
C language - modularization -clion (static library, dynamic library) use
Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
VIM use
Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
QT -- create QT program
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
How to make enterprise recruitment QR code?
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
为什么更新了 DNS 记录不生效?
Nacos microservice gateway component +swagger2 interface generation
Codeforces Round #643 (Div. 2)——B. Young Explorers
Gbase observation | how to protect the security of information system with frequent data leakage
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!