当前位置:网站首页>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).
边栏推荐
- FFT 学习笔记(自认为详细)
- 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?
- 15 MySQL stored procedures and functions
- 云呐|固定资产管理系统主要操作流程有哪些
- USB Interface USB protocol
- FFMPEG关键结构体——AVFrame
- Key structure of ffmpeg -- AVCodecContext
- Senparc. Weixin. Sample. MP source code analysis
- XML configuration file (DTD detailed explanation)
- 【luogu P3295】萌萌哒(并查集)(倍增)
猜你喜欢

【DesignMode】装饰者模式(Decorator pattern)

Transport layer protocol ----- UDP protocol

上门预约服务类的App功能详解

FFMPEG关键结构体——AVCodecContext

Tips for using pads router

CAS and synchronized knowledge

Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4

My colleagues quietly told me that flying Book notification can still play like this

The use of El cascader and the solution of error reporting

Redis high availability - master-slave replication, sentinel mode, cluster
随机推荐
硬件及接口学习总结
There is no network after configuring the agent by capturing packets with Fiddler mobile phones
【QT】Qt使用QJson生成json文件并保存
How much do you know about the bank deposit business that software test engineers must know?
Problem solving win10 quickly open ipynb file
Transport layer protocol ----- UDP protocol
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
MySQL global lock and table lock
数据库遇到的问题
云呐|固定资产管理系统主要操作流程有哪些
Key structure of ffmpeg - avframe
Open source CRM customer relationship system management system source code, free sharing
MySql——CRUD
剖面测量之提取剖面数据
Use CAS instead of synchronized
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
Browser local storage
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
[designmode] adapter pattern