首页 > 其他分享 >leetcode-2169-easy

leetcode-2169-easy

时间:2023-12-06 09:03:33浏览次数:23  
标签:operations 10 num1 num2 Since 2169 easy subtract leetcode

Count Operations to Obtain Zero
You are given two non-negative integers num1 and num2.

In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.

For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1.
Return the number of operations required to make either num1 = 0 or num2 = 0.

Example 1:

Input: num1 = 2, num2 = 3
Output: 3
Explanation: 
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
So the total number of operations required is 3.
Example 2:

Input: num1 = 10, num2 = 10
Output: 1
Explanation: 
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
So the total number of operations required is 1.
Constraints:

0 <= num1, num2 <= 105

思路一:模拟

    public int countOperations(int num1, int num2) {
        int n = 0;

        while (num1 > 0 && num2 > 0) {
            if (num1 >= num2) {
                num1 -= num2;
            } else {
                num2 -= num1;
            }

            n++;
        }

        return n;
    }

标签:operations,10,num1,num2,Since,2169,easy,subtract,leetcode
From: https://www.cnblogs.com/iyiluo/p/17878738.html

相关文章

  • leetcode-1646-easy
    GetMaximuminGeneratedArrayYouaregivenanintegern.A0-indexedintegerarraynumsoflengthn+1isgeneratedinthefollowingway:nums[0]=0nums[1]=1nums[2*i]=nums[i]when2<=2*i<=nnums[2*i+1]=nums[i]+nums[i+1]......
  • leetcode-1464-easy
    MaximumProductofTwoElementsinanArrayGiventhearrayofintegersnums,youwillchoosetwodifferentindicesiandjofthatarray.Returnthemaximumvalueof(nums[i]-1)*(nums[j]-1).Example1:Input:nums=[3,4,5,2]Output:12Explanation:I......
  • leetcode-1455-easy
    CheckIfaWordOccursAsaPrefixofAnyWordinaSentenceGivenasentencethatconsistsofsomewordsseparatedbyasinglespace,andasearchWord,checkifsearchWordisaprefixofanywordinsentence.Returntheindexofthewordinsentence(1-......
  • leetcode-2180-easy
    CountIntegersWithEvenDigitSumGivenapositiveintegernum,returnthenumberofpositiveintegerslessthanorequaltonumwhosedigitsumsareeven.Thedigitsumofapositiveintegeristhesumofallitsdigits.Example1:Input:num=4Ou......
  • leetcode-1572-easy
    MatrixDiagonalSumGivenasquarematrixmat,returnthesumofthematrixdiagonals.Onlyincludethesumofalltheelementsontheprimarydiagonalandalltheelementsonthesecondarydiagonalthatarenotpartoftheprimarydiagonal.Example1:......
  • leetcode-1550-easy
    ThreeConsecutiveOddsGivenanintegerarrayarr,returntrueiftherearethreeconsecutiveoddnumbersinthearray.Otherwise,returnfalse.Example1:Input:arr=[2,6,4,1]Output:falseExplanation:Therearenothreeconsecutiveodds.Example2:......
  • leetcode-1512-easy
    NumberofGoodPairsGivenanarrayofintegersnums,returnthenumberofgoodpairs.Apair(i,j)iscalledgoodifnums[i]==nums[j]andi<j.Example1:Input:nums=[1,2,3,1,1,3]Output:4Explanation:Thereare4goodpairs(0,3),(0,4),(......
  • CF1824B1 LuoTianyi and the Floating Islands (Easy Version) 题解
    题意:思路:由于$k∈[1,3]$,分类讨论:当$k=1$时,有人结点自身即为好结点,每种情况的期望为$\frac{1}{n}$,$n$种情况的期望和为$1$。最终答案即为$1$。当$k=2$时,$2$个有人结点之间的路径上的结点即为好结点,那么问题转化为:树上所有路径的结点......
  • C1. Good Subarrays (Easy Version)
    思路:我们枚举每一个左端点,对于每一个左端点,寻找最长的满足条件的区间,这个区间长度就是左端点对答案的贡献,可以发现具有单调性,右端点只会前进不会倒退。所以我们两个指针各扫一遍区间就可以。#include<bits/stdc++.h>#definelsp<<1#definersp<<1|1#definePIIpair<int,......
  • [LeetCode Hot 100] LeetCode19. 删除链表的倒数第N个结点
    题目描述思路一:采用两次遍历第一遍遍历先获取链表的长度length第二次从dummy节点开始走length-n步然后将该节点指向下下个节点思路二:采用一次遍历设置虚拟节点dummyHead指向head设定双指针p和q,初始都指向虚拟节点dummyHead移动q,直到p与q之间相隔的元素个数为n(即q走......