Skip to content

Commit 2b1cb32

Browse files
committed
add 2169
1 parent 4c0685c commit 2b1cb32

2 files changed

Lines changed: 42 additions & 32 deletions

File tree

javascript/LeetCode/Array/2169.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2169. Count Operations to Obtain Zero
3+
*
4+
* 一次操作中,若nums1 > nums2,則nums1 = nums1 - nums2
5+
* nums1 < nums2, nums2 = nums1 - nums2
6+
* 計算要幾次才能使得nums1 = 0 or num2 = 0
7+
*
8+
* @param {number} num1
9+
* @param {number} num2
10+
* @return {number}
11+
*/
12+
var countOperations = function(num1, num2) {
13+
// solution 1.
14+
// let ans = 0;
15+
// while(num1 !== 0 && num2 !== 0){
16+
// ans += Math.floor(num1 / num2);
17+
// num1 %= num2;
18+
// [num1,num2] = [num2,num1];
19+
// }
20+
// return ans;
21+
22+
// solution 2.
23+
let ans = 0;
24+
while(num1 !== 0 && num2 !== 0){
25+
ans += Math.floor(num1 / num2);
26+
num1 %= num2;
27+
// swap nums1 and nums2
28+
let temp = num1;
29+
num1 = num2;
30+
num2 = temp;
31+
}
32+
return ans;
33+
};
34+
let num1 = 2, num2 = 3;
35+
// Output: 3
36+
// Explanation:
37+
// - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
38+
// - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
39+
// - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
40+
// Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
41+
// So the total number of operations required is 3.
42+
console.log(countOperations(num1,num2))

javascript/index.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,35 +1079,3 @@ var findXSum = function(nums, k, x) {
10791079
// console.log(findXSum(nums,k,x));
10801080

10811081

1082-
/**
1083-
* 2169. Count Operations to Obtain Zero
1084-
*
1085-
* 一次操作中,若nums1 >= nums2,則nums1 = nums1 - nums2,否則nums2 = nums1 - nums2
1086-
* 計算要幾次才能使得nums1 = 0 or num2 = 0
1087-
*
1088-
* @param {number} num1
1089-
* @param {number} num2
1090-
* @return {number}
1091-
*/
1092-
var countOperations = function(num1, num2) {
1093-
let ans = 0;
1094-
while(num1 !== 0 || num2 !== 0){
1095-
if(num1 > num2){
1096-
num1 = num1 - num2;
1097-
ans++;
1098-
}else{
1099-
num2 = num1 - num2;
1100-
ans++;
1101-
}
1102-
}
1103-
return ans;
1104-
};
1105-
let num1 = 2, num2 = 3
1106-
// Output: 3
1107-
// Explanation:
1108-
// - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
1109-
// - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
1110-
// - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
1111-
// Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
1112-
// So the total number of operations required is 3.
1113-
console.log(countOperations(num1,num2))

0 commit comments

Comments
 (0)