当前位置:网站首页>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;
}
};
边栏推荐
- MQTT 协议中文版
- Quickly build oncyber io
- np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
- Win10 professional edition user name modification
- Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index
- 数组,整型,字符变量在全局和局部的存在形式
- 93. 獲得內網的所有IP地址
- JS string combination
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- 2021-02-12
猜你喜欢

验收标准到底是不是测试用例?

Properties Chinese garbled code

QT custom window fillets

Redis (II) Memory mapped data structure

MySQL 7 affair

JVM (VI) Virtual machine bytecode execution engine (with stack execution process and bytecode instruction table)

Solution to invalid small program scroll into view

2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
![[experiment] MySQL master-slave replication and read-write separation](/img/aa/7d0799013ff749cacf44ba3b773dff.png)
[experiment] MySQL master-slave replication and read-write separation
![[MySQL] index invalidation and index optimization](/img/af/9ed540aab764f4962ccc9bbfe406f3.jpg)
[MySQL] index invalidation and index optimization
随机推荐
Cookie object
MySQL 4 Database table storage structure & tablespace
High performance computing framework for image processing
不需要安装防毒软件的系统Chromebook
Yarn scheduling
用于图像处理的高性能计算框架
[Wayland] Wayland introduction and customized guidance
93. obtain all IP addresses of the Intranet
【ParquetEncodingException: empty fields are illegal, the field should be ommited completely instead
Introduction to encoding formats (ASCII, Unicode and UTF-8)
2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
Love and hate in the Jianghu
Dynamic proxy
Student management system
Code types and data structures corresponding to the five object types of redis
Redis (II) Memory mapped data structure
PHP interface generates cache and MD5 encryption uniformly
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
Circuitbreaker fuse of resilience4j - circuitbreakerevent event
np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍