当前位置:网站首页>Leetcode 2169. 得到 0 的操作数
Leetcode 2169. 得到 0 的操作数
2022-06-12 10:21:00 【我不是萧海哇~~~~】

给你两个 非负 整数 num1 和 num2 。
每一步 操作 中,如果 num1 >= num2 ,你必须用 num1 减 num2 ;否则,你必须用 num2 减 num1 。
- 例如,num1 = 5 且 num2 = 4 ,应该用 num1 减 num2 ,因此,得到 num1 = 1 和 num2 = 4
。然而,如果 num1 = 4且 num2 = 5 ,一步操作后,得到 num1 = 4 和 num2 = 1 。
返回使 num1 = 0 或 num2 = 0 的 操作数 。
示例 1:
输入:num1 = 2, num2 = 3
输出:3
解释:
- 操作 1 :num1 = 2 ,num2 = 3 。由于 num1 < num2 ,num2 减 num1 得到 num1 = 2 ,num2 = 3 - 2 = 1 。
- 操作 2 :num1 = 2 ,num2 = 1 。由于 num1 > num2 ,num1 减 num2 。
- 操作 3 :num1 = 1 ,num2 = 1 。由于 num1 == num2 ,num1 减 num2 。
此时 num1 = 0 ,num2 = 1 。由于 num1 == 0 ,不需要再执行任何操作。
所以总操作数是 3 。
示例 2:
输入:num1 = 10, num2 = 10
输出:1
解释:
- 操作 1 :num1 = 10 ,num2 = 10 。由于 num1 == num2 ,num1 减 num2 得到 num1 = 10 - 10 = 0 。
此时 num1 = 0 ,num2 = 10 。由于 num1 == 0 ,不需要再执行任何操作。
所以总操作数是 1 。
提示:
- 0 <= num1, num2 <= 10^5
Code:
class Solution {
public:
int countOperations(int num1, int num2) {
int res=0;
if(num1==0 || num2==0)
return res;
do
{
if(num1>=num2)
num1-=num2;
else
num2-=num1;
res++;
if(num1==0 || num2==0)
return res;
}while(1);
return res;
}
};
边栏推荐
- 在App内跳转微信,打开微信
- np. Meshgrid() function and coordinate position generation in 3D space and numpy Introduction to repeat() function
- np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
- [CEGUI] log system
- 2022淘宝618超级喵运会玩法攻略 618超级喵运会玩法技巧
- Cookie object
- Pagoda chevereto1.6.2 the latest version of stepping on the pit tutorial in Chinese
- C break continue return
- Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
- ID obfuscation
猜你喜欢

2021-03-26

2022京东618预售定金怎么退?京东618定金能退吗?

JVM (IV) Class file structure (complete parsing of bytecode attached)

MySQL v Index and algorithm
![[926. flip the string to monotonic increment]](/img/fa/1838405693aa0998694d4895a611c2.png)
[926. flip the string to monotonic increment]

How to implement Web3.0 and digital fashion?

IoT简介

Mqtt protocol Chinese version

Qt自定义窗口圆角

MySQL 4 Database table storage structure & tablespace
随机推荐
Wechat payment, wechat refund, Alibaba payment
Spark complex structure data retrieval method
tp6调试(trace)
2021-03-26
93. Obtenir toutes les adresses IP de l'Intranet
1268_FreeRTOS任务上下文切换的实现
How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games
CentOS 7 installing MySQL 8
93. 獲得內網的所有IP地址
93. obtain all IP addresses of the Intranet
Dynamic proxy
Halcon combined with C # to detect surface defects -- affine transformation (III)
Circuitbreaker fuse of resilience4j - Summary
[CEGUI] log system
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
np. Meshgrid() function and coordinate position generation in 3D space and numpy Introduction to repeat() function
PHP curl function
How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
2022京东618预售定金怎么退?京东618定金能退吗?
Pycharm view the current version of opencv