当前位置:网站首页>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
边栏推荐
- 云原生应用开发之 gRPC 入门
- How to use diffusion models for interpolation—— Principle analysis and code practice
- leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
- 常见的磁盘格式以及它们之间的区别
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- 数据链路层及网络层协议要点
- leetcode 866. Prime Palindrome | 866. prime palindromes
- I don't know. The real interest rate of Huabai installment is so high
- ClickHouse原理解析与应用实践》读书笔记(8)
- 微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
猜你喜欢
Talk about the realization of authority control and transaction record function of SAP system
Introduction à l'outil nmap et aux commandes communes
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
[knowledge map paper] r2d2: knowledge map reasoning based on debate dynamics
力争做到国内赛事应办尽办,国家体育总局明确安全有序恢复线下体育赛事
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
burpsuite
Talk about the cloud deployment of local projects created by SAP IRPA studio
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
Keras' deep learning practice -- gender classification based on inception V3
随机推荐
VR/AR 的产业发展与技术实现
直接加比较合适
Ml backward propagation
发现值守设备被攻击后分析思路
JVM memory and garbage collection-3-runtime data area / heap area
metasploit
Deeppath: a reinforcement learning method of knowledge graph reasoning
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.1 data operation_ Learning thinking and exercise answers
Analysis ideas after discovering that the on duty equipment is attacked
Height of life
力扣6_1342. 将数字变成 0 的操作次数
Redisson分布式锁解锁异常
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.2 data preprocessing_ Learning thinking and exercise answers
Semantic segmentation | learning record (4) expansion convolution (void convolution)
Installing and using mpi4py
Introduction to ADB tools
Kwai applet guaranteed payment PHP source code packaging
Introduction à l'outil nmap et aux commandes communes
生命的高度
2022国内十大工业级三维视觉引导企业一览