当前位置:网站首页>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).
边栏推荐
- 微信小程序---WXML 模板语法(附带笔记文档)
- Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
- 软件测试工程师必会的银行存款业务,你了解多少?
- [designmode] composite mode
- FFT learning notes (I think it is detailed)
- 妙才周刊 - 8
- Detailed explanation of APP functions of door-to-door appointment service
- FFT 学习笔记(自认为详细)
- 多普勒效應(多普勒頻移)
- Key structure of ffmpeg -- AVCodecContext
猜你喜欢
Detailed explanation of APP functions of door-to-door appointment service
上门预约服务类的App功能详解
MySql——CRUD
Tools to improve work efficiency: the idea of SQL batch generation tools
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
Hardware and interface learning summary
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
Hudi of data Lake (2): Hudi compilation
C reflection and type
FFT learning notes (I think it is detailed)
随机推荐
云呐|固定资产管理系统功能包括哪些?
There is no network after configuring the agent by capturing packets with Fiddler mobile phones
【GYM 102832H】【模板】Combination Lock(二分图博弈)
Qt QPushButton详解
MySql——CRUD
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
NSSA area where OSPF is configured for Huawei equipment
7.5 装饰器
Permission problem: source bash_ profile permission denied
【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
Knowledge about the memory size occupied by the structure
[designmode] composite mode
7.5 simulation summary
The use of El cascader and the solution of error reporting
Upgrade openssl-1.1.1p for openssl-1.0.2k
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
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
用列表初始化你的vector&&initializer_list简介
Tips for using pads router
China Jinmao online electronic signature, accelerating the digitization of real estate business