当前位置:网站首页>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).
边栏推荐
- 妙才周刊 - 8
- 7.5 装饰器
- 14 MySQL view
- Tools to improve work efficiency: the idea of SQL batch generation tools
- Senparc. Weixin. Sample. MP source code analysis
- mysql-全局锁和表锁
- 18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
- FFMPEG关键结构体——AVFormatContext
- Open source CRM customer relationship system management system source code, free sharing
- Determinant learning notes (I)
猜你喜欢

Hudi of data Lake (2): Hudi compilation

MySQL之函数

Problems encountered in the database

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

PV静态创建和动态创建
![Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]

云呐|固定资产管理系统功能包括哪些?

用列表初始化你的vector&&initializer_list简介

How much do you know about the bank deposit business that software test engineers must know?

【DesignMode】组合模式(composite mode)
随机推荐
FFMPEG关键结构体——AVFormatContext
上门预约服务类的App功能详解
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
MySQL global lock and table lock
CAS and synchronized knowledge
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
JS 这次真的可以禁止常量修改了!
Transport layer protocol ----- UDP protocol
Problem solving win10 quickly open ipynb file
Ffmpeg learning - core module
PV static creation and dynamic creation
Key structure of ffmpeg - avframe
FFmpeg学习——核心模块
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Configuring OSPF GR features for Huawei devices
7.5 simulation summary