当前位置:网站首页>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).
边栏推荐
- Learn PWN from CTF wiki - ret2libc1
- Single merchant v4.4 has the same original intention and strength!
- 时区的区别及go语言的time库
- mysql-全局锁和表锁
- Knowledge about the memory size occupied by the structure
- MySql——CRUD
- openssl-1.0.2k版本升级openssl-1.1.1p
- Redis high availability - master-slave replication, sentinel mode, cluster
- 认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
- GD32F4xx uIP协议栈移植记录
猜你喜欢
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
多普勒效應(多普勒頻移)
FFT learning notes (I think it is detailed)
Laser slam learning record
提升工作效率工具:SQL批量生成工具思想
云呐|公司固定资产管理系统有哪些?
The use of El cascader and the solution of error reporting
PV static creation and dynamic creation
PV静态创建和动态创建
随机推荐
【DesignMode】组合模式(composite mode)
VBA fast switching sheet
Senparc. Weixin. Sample. MP source code analysis
CloudCompare&PCL 点云随机添加噪声
【GYM 102832H】【模板】Combination Lock(二分图博弈)
Transport layer protocol ----- UDP protocol
Qt 一个简单的word文档编辑器
【QT】Qt使用QJson生成json文件并保存
7.5 decorator
Configuring OSPF GR features for Huawei devices
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
FFMPEG关键结构体——AVFormatContext
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Open source CRM customer relationship system management system source code, free sharing
Wechat applet -- wxml template syntax (with notes)
Laser slam learning record
Huawei equipment configuration ospf-bgp linkage
CAS and synchronized knowledge
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
Redis high availability - master-slave replication, sentinel mode, cluster