当前位置:网站首页>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 = 5
Andnum2 = 4
, Should use thenum1
reducenum2
, therefore , obtainnum1 = 1
andnum2 = 4
. However , Ifnum1 = 4
Andnum2 = 5
, After one step operation , obtainnum1 = 4
andnum2 = 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).
边栏推荐
- There is no network after configuring the agent by capturing packets with Fiddler mobile phones
- 20220703 week race: number of people who know the secret - dynamic rules (problem solution)
- CAS and synchronized knowledge
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- Gd32f4xx UIP protocol stack migration record
- [Luogu p3295] mengmengda (parallel search) (double)
- QT a simple word document editor
- PV静态创建和动态创建
- FFT 学习笔记(自认为详细)
- Key structure of ffmpeg - avformatcontext
猜你喜欢
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
openssl-1.0.2k版本升级openssl-1.1.1p
How much do you know about the bank deposit business that software test engineers must know?
My colleagues quietly told me that flying Book notification can still play like this
MySQL之函数
There is no network after configuring the agent by capturing packets with Fiddler mobile phones
跟着CTF-wiki学pwn——ret2libc1
权限问题:source .bash_profile permission denied
Problem solving win10 quickly open ipynb file
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
随机推荐
PV静态创建和动态创建
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Senparc.Weixin.Sample.MP源码剖析
wx. Getlocation (object object) application method, latest version
Convert Chinese into pinyin
mysql-全局锁和表锁
7.5 装饰器
7.5 decorator
XML配置文件(DTD详细讲解)
Mathematical model Lotka Volterra
What are the functions of Yunna fixed assets management system?
JS 这次真的可以禁止常量修改了!
18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
Hardware and interface learning summary
MySQL global lock and table lock
Which side projects can be achieved? Is it difficult for we media to earn more than 10000 a month?
Qt 一个简单的word文档编辑器
第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
MySQL functions