当前位置:网站首页>Force buckle 6_ 1342. Number of operations to change a number to 0
Force buckle 6_ 1342. Number of operations to change a number to 0
2022-07-08 02:15:00 【Don't sleep in class】
Give you a nonnegative integer num , Please go back and turn it into 0 The number of steps required . If the current number is even , You need to divide it by 2 ; otherwise , subtract 1 .
Example 1:
Input :num = 14
Output :6
explain :
step 1) 14 It's even , Divide 2 obtain 7 .
step 2) 7 Is odd , reduce 1 obtain 6 .
step 3) 6 It's even , Divide 2 obtain 3 .
step 4) 3 Is odd , reduce 1 obtain 2 .
step 5) 2 It's even , Divide 2 obtain 1 .
step 6) 1 Is odd , reduce 1 obtain 0 .
Example 2:
Input :num = 8
Output :4
explain :
step 1) 8 It's even , Divide 2 obtain 4 .
step 2) 4 It's even , Divide 2 obtain 2 .
step 3) 2 It's even , Divide 2 obtain 1 .
step 4) 1 Is odd , reduce 1 obtain 0 .
Example 3:
Input :num = 123
Output :12
source : Power button (LeetCode)
The principles used in both solutions are based on the characteristics of binary numbers , According to the 0 or 1 Determine the corresponding number of steps .
Java solution
class Solution {
public int numberOfSteps(int num) {
int ret = 0;
while (num > 0) {
ret += (num > 1 ? 1 : 0) + (num & 0x01);
// take & The numbers on both sides of the symbol are converted into binary , To perform bitwise operations
num >>= 1;
}
return ret;
}
}
Python solution
class Solution:
def numberOfSteps(self, num: int) -> int:
ans = 0
while num:
ans += num & 1
if num > 1:
ans += 1
num >>= 1
return ans
边栏推荐
- 谈谈 SAP 系统的权限管控和事务记录功能的实现
- 《通信软件开发与应用》课程结业报告
- Completion report of communication software development and Application
- 发现值守设备被攻击后分析思路
- Where to think
- Version 2.0 of tapdata, the open source live data platform, has been released
- Alo who likes TestMan
- 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)
- Leetcode featured 200 channels -- array article
- Redission源码解析
猜你喜欢
力争做到国内赛事应办尽办,国家体育总局明确安全有序恢复线下体育赛事
系统测试的类型有哪些,我给你介绍
Matlab r2021b installing libsvm
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
nmap工具介紹及常用命令
Unity 射线与碰撞范围检测【踩坑记录】
Semantic segmentation | learning record (3) FCN
牛熊周期与加密的未来如何演变?看看红杉资本怎么说
MQTT X Newsletter 2022-06 | v1.8.0 发布,新增 MQTT CLI 和 MQTT WebSocket 工具
随机推荐
XMeter Newsletter 2022-06|企业版 v3.2.3 发布,错误日志与测试报告图表优化
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
Ml self realization / logistic regression / binary classification
Nmap tool introduction and common commands
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
Semantic segmentation | learning record (3) FCN
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
[knowledge map] interpretable recommendation based on knowledge map through deep reinforcement learning
《通信软件开发与应用》课程结业报告
Literature reading and writing
微软 AD 超基础入门
Redisson distributed lock unlocking exception
科普 | 什么是灵魂绑定代币SBT?有何价值?
leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
Uniapp one click Copy function effect demo (finishing)
Talk about the cloud deployment of local projects created by SAP IRPA studio
鱼和虾走的路
Disk rust -- add a log to the program
Introduction to Microsoft ad super Foundation
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“