当前位置:网站首页>LeetCode 6004. Get operands of 0
LeetCode 6004. Get operands of 0
2022-07-06 00:09:00 【Daylight629】
6004. obtain 0 The number of operations
Here are two for you non-negative Integers num1 and num2 .
Each step operation in , If num1 >= num2 , You have to use num1 reduce num2 ; otherwise , You have to use num2 reduce num1 .
- for example ,
num1 = 5Andnum2 = 4, Should use thenum1reducenum2, therefore , obtainnum1 = 1andnum2 = 4. However , Ifnum1 = 4Andnum2 = 5, After one step operation , obtainnum1 = 4andnum2 = 1.
Return to make num1 = 0 or num2 = 0 Of Operands .
Example 1:
Input :num1 = 2, num2 = 3
Output :3
explain :
- operation 1 :num1 = 2 ,num2 = 3 . because num1 < num2 ,num2 reduce num1 obtain num1 = 2 ,num2 = 3 - 2 = 1 .
- operation 2 :num1 = 2 ,num2 = 1 . because num1 > num2 ,num1 reduce num2 .
- operation 3 :num1 = 1 ,num2 = 1 . because num1 == num2 ,num1 reduce num2 .
here num1 = 0 ,num2 = 1 . because num1 == 0 , No more action is required .
So the total operand is 3 .
Example 2:
Input :num1 = 10, num2 = 10
Output :1
explain :
- operation 1 :num1 = 10 ,num2 = 10 . because num1 == num2 ,num1 reduce num2 obtain num1 = 10 - 10 = 0 .
here num1 = 0 ,num2 = 10 . because num1 == 0 , No more action is required .
So the total operand is 1 .
Tips :
0 <= num1, num2 <= 105
Two 、 Method 1
simulation , That is, division by turns
class Solution {
public int countOperations(int num1, int num2) {
int res = 0;
while (num1 != 0 && num2 != 0) {
if (num1 >= num2) {
num1 -= num2;
} else {
num2 -= num1;
}
res++;
}
return res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(1).
边栏推荐
- 关于slmgr命令的那些事
- 第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- Configuring OSPF load sharing for Huawei devices
- XML configuration file (DTD detailed explanation)
- JS can really prohibit constant modification this time!
- 用列錶初始化你的vector&&initializer_list簡介
- 时区的区别及go语言的time库
- 20220703 week race: number of people who know the secret - dynamic rules (problem solution)
- 【GYM 102832H】【模板】Combination Lock(二分图博弈)
猜你喜欢
随机推荐
VBA fast switching sheet
Detailed explanation of APP functions of door-to-door appointment service
Hudi of data Lake (2): Hudi compilation
Miaochai Weekly - 8
2022.7.5-----leetcode.729
Redis high availability - master-slave replication, sentinel mode, cluster
Senparc. Weixin. Sample. MP source code analysis
数据库遇到的问题
时区的区别及go语言的time库
Senparc.Weixin.Sample.MP源码剖析
Hudi of data Lake (1): introduction to Hudi
权限问题:source .bash_profile permission denied
[gym 102832h] [template] combination lock (bipartite game)
多普勒效應(多普勒頻移)
行列式学习笔记(一)
上门预约服务类的App功能详解
The use of El cascader and the solution of error reporting
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
Learn PWN from CTF wiki - ret2libc1
用列錶初始化你的vector&&initializer_list簡介








