diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README.md b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README.md index da96e150a5a4d..fe575868c1745 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README.md +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README.md @@ -56,7 +56,15 @@ tags: -### 方法一 +### 方法一:计数 + +我们首先用一个数组 $\textit{cnt}$ 记录 $\textit{num}$ 中每个数字的出现次数。 + +如果 $\textit{num}$ 是负数,那么数字应该按照从大到小的顺序排列,因此我们从 $9$ 到 $0$ 遍历 $\textit{cnt}$,将数字按照出现次数从大到小的顺序排列。 + +如果是正数,我们首先找到第一个非 $0$ 的数字,将其放在第一位,然后按照从小到大的顺序排列剩余的数字。 + +时间复杂度 $O(\log n)$,其中 $n$ 为数字 $\textit{num}$ 的大小。空间复杂度 $O(1)$。 @@ -65,30 +73,30 @@ tags: ```python class Solution: def smallestNumber(self, num: int) -> int: - if num == 0: - return 0 - cnt = [0] * 10 neg = num < 0 num = abs(num) + cnt = [0] * 10 while num: - num, v = divmod(num, 10) - cnt[v] += 1 - ans = "" + cnt[num % 10] += 1 + num //= 10 + ans = 0 if neg: - for i in range(9, -1, -1): - if cnt[i]: - ans += str(i) * cnt[i] - return -int(ans) + for i in reversed(range(10)): + for _ in range(cnt[i]): + ans *= 10 + ans += i + return -ans if cnt[0]: for i in range(1, 10): if cnt[i]: - ans += str(i) + ans = i cnt[i] -= 1 break for i in range(10): - if cnt[i]: - ans += str(i) * cnt[i] - return int(ans) + for _ in range(cnt[i]): + ans *= 10 + ans += i + return ans ``` #### Java @@ -96,21 +104,19 @@ class Solution: ```java class Solution { public long smallestNumber(long num) { - if (num == 0) { - return 0; - } - int[] cnt = new int[10]; boolean neg = num < 0; num = Math.abs(num); - while (num != 0) { - cnt[(int) (num % 10)]++; + int[] cnt = new int[10]; + while (num > 0) { + ++cnt[(int) (num % 10)]; num /= 10; } long ans = 0; if (neg) { for (int i = 9; i >= 0; --i) { - while (cnt[i]-- > 0) { + while (cnt[i] > 0) { ans = ans * 10 + i; + --cnt[i]; } } return -ans; @@ -118,15 +124,16 @@ class Solution { if (cnt[0] > 0) { for (int i = 1; i < 10; ++i) { if (cnt[i] > 0) { - ans = ans * 10 + i; - cnt[i]--; + --cnt[i]; + ans = i; break; } } } for (int i = 0; i < 10; ++i) { - while (cnt[i]-- > 0) { + while (cnt[i] > 0) { ans = ans * 10 + i; + --cnt[i]; } } return ans; @@ -140,31 +147,38 @@ class Solution { class Solution { public: long long smallestNumber(long long num) { - if (num == 0) return 0; - vector cnt(10); bool neg = num < 0; num = abs(num); - while (num) { - cnt[num % 10]++; + int cnt[10]{}; + while (num > 0) { + ++cnt[num % 10]; num /= 10; } long long ans = 0; if (neg) { - for (int i = 9; i >= 0; --i) - while (cnt[i]--) ans = ans * 10 + i; + for (int i = 9; i >= 0; --i) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + --cnt[i]; + } + } return -ans; } if (cnt[0]) { for (int i = 1; i < 10; ++i) { - if (cnt[i]) { - ans = ans * 10 + i; - cnt[i]--; + if (cnt[i] > 0) { + --cnt[i]; + ans = i; break; } } } - for (int i = 0; i < 10; ++i) - while (cnt[i]--) ans = ans * 10 + i; + for (int i = 0; i < 10; ++i) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + --cnt[i]; + } + } return ans; } }; @@ -173,46 +187,188 @@ public: #### Go ```go -func smallestNumber(num int64) int64 { - if num == 0 { - return 0 - } - cnt := make([]int, 10) +func smallestNumber(num int64) (ans int64) { neg := num < 0 - if neg { - num = -num - } - for num != 0 { + num = max(num, -num) + cnt := make([]int, 10) + + for num > 0 { cnt[num%10]++ num /= 10 } - ans := 0 + if neg { for i := 9; i >= 0; i-- { - for j := 0; j < cnt[i]; j++ { - ans = ans*10 + i + for cnt[i] > 0 { + ans = ans*10 + int64(i) + cnt[i]-- } } - return -int64(ans) + return -ans } + if cnt[0] > 0 { for i := 1; i < 10; i++ { if cnt[i] > 0 { - ans = ans*10 + i cnt[i]-- + ans = int64(i) break } } } + for i := 0; i < 10; i++ { - for j := 0; j < cnt[i]; j++ { - ans = ans*10 + i + for cnt[i] > 0 { + ans = ans*10 + int64(i) + cnt[i]-- } } - return int64(ans) + + return ans +} +``` + +#### TypeScript + +```ts +function smallestNumber(num: number): number { + const neg = num < 0; + num = Math.abs(num); + const cnt = Array(10).fill(0); + + while (num > 0) { + cnt[num % 10]++; + num = Math.floor(num / 10); + } + + let ans = 0; + if (neg) { + for (let i = 9; i >= 0; i--) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + return -ans; + } + + if (cnt[0] > 0) { + for (let i = 1; i < 10; i++) { + if (cnt[i] > 0) { + cnt[i]--; + ans = i; + break; + } + } + } + + for (let i = 0; i < 10; i++) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + + return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_number(num: i64) -> i64 { + let mut neg = num < 0; + let mut num = num.abs(); + let mut cnt = vec![0; 10]; + + while num > 0 { + cnt[(num % 10) as usize] += 1; + num /= 10; + } + + let mut ans = 0; + if neg { + for i in (0..10).rev() { + while cnt[i] > 0 { + ans = ans * 10 + i as i64; + cnt[i] -= 1; + } + } + return -ans; + } + + if cnt[0] > 0 { + for i in 1..10 { + if cnt[i] > 0 { + cnt[i] -= 1; + ans = i as i64; + break; + } + } + } + + for i in 0..10 { + while cnt[i] > 0 { + ans = ans * 10 + i as i64; + cnt[i] -= 1; + } + } + + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} num + * @return {number} + */ +var smallestNumber = function (num) { + const neg = num < 0; + num = Math.abs(num); + const cnt = Array(10).fill(0); + + while (num > 0) { + cnt[num % 10]++; + num = Math.floor(num / 10); + } + + let ans = 0; + if (neg) { + for (let i = 9; i >= 0; i--) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + return -ans; + } + + if (cnt[0] > 0) { + for (let i = 1; i < 10; i++) { + if (cnt[i] > 0) { + cnt[i]--; + ans = i; + break; + } + } + } + + for (let i = 0; i < 10; i++) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + + return ans; +}; +``` + diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md index 54cdc3fb112a4..41688c0737c48 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md @@ -31,7 +31,7 @@ tags:
 Input: num = 310
 Output: 103
-Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310. 
+Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310.
 The arrangement with the smallest value that does not contain any leading zeros is 103.
 
@@ -57,7 +57,15 @@ The arrangement with the smallest value that does not contain any leading zeros -### Solution 1 +### Solution 1: Counting + +We first use an array $\textit{cnt}$ to record the number of occurrences of each digit in $\textit{num}$. + +If $\textit{num}$ is negative, the digits should be arranged in descending order. Therefore, we traverse $\textit{cnt}$ from $9$ to $0$ and arrange the digits in descending order according to their occurrences. + +If $\textit{num}$ is positive, we first find the first non-zero digit and place it in the first position, then arrange the remaining digits in ascending order. + +The time complexity is $O(\log n)$, where $n$ is the size of the number $\textit{num}$. The space complexity is $O(1)$. @@ -66,30 +74,30 @@ The arrangement with the smallest value that does not contain any leading zeros ```python class Solution: def smallestNumber(self, num: int) -> int: - if num == 0: - return 0 - cnt = [0] * 10 neg = num < 0 num = abs(num) + cnt = [0] * 10 while num: - num, v = divmod(num, 10) - cnt[v] += 1 - ans = "" + cnt[num % 10] += 1 + num //= 10 + ans = 0 if neg: - for i in range(9, -1, -1): - if cnt[i]: - ans += str(i) * cnt[i] - return -int(ans) + for i in reversed(range(10)): + for _ in range(cnt[i]): + ans *= 10 + ans += i + return -ans if cnt[0]: for i in range(1, 10): if cnt[i]: - ans += str(i) + ans = i cnt[i] -= 1 break for i in range(10): - if cnt[i]: - ans += str(i) * cnt[i] - return int(ans) + for _ in range(cnt[i]): + ans *= 10 + ans += i + return ans ``` #### Java @@ -97,21 +105,19 @@ class Solution: ```java class Solution { public long smallestNumber(long num) { - if (num == 0) { - return 0; - } - int[] cnt = new int[10]; boolean neg = num < 0; num = Math.abs(num); - while (num != 0) { - cnt[(int) (num % 10)]++; + int[] cnt = new int[10]; + while (num > 0) { + ++cnt[(int) (num % 10)]; num /= 10; } long ans = 0; if (neg) { for (int i = 9; i >= 0; --i) { - while (cnt[i]-- > 0) { + while (cnt[i] > 0) { ans = ans * 10 + i; + --cnt[i]; } } return -ans; @@ -119,15 +125,16 @@ class Solution { if (cnt[0] > 0) { for (int i = 1; i < 10; ++i) { if (cnt[i] > 0) { - ans = ans * 10 + i; - cnt[i]--; + --cnt[i]; + ans = i; break; } } } for (int i = 0; i < 10; ++i) { - while (cnt[i]-- > 0) { + while (cnt[i] > 0) { ans = ans * 10 + i; + --cnt[i]; } } return ans; @@ -141,31 +148,38 @@ class Solution { class Solution { public: long long smallestNumber(long long num) { - if (num == 0) return 0; - vector cnt(10); bool neg = num < 0; num = abs(num); - while (num) { - cnt[num % 10]++; + int cnt[10]{}; + while (num > 0) { + ++cnt[num % 10]; num /= 10; } long long ans = 0; if (neg) { - for (int i = 9; i >= 0; --i) - while (cnt[i]--) ans = ans * 10 + i; + for (int i = 9; i >= 0; --i) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + --cnt[i]; + } + } return -ans; } if (cnt[0]) { for (int i = 1; i < 10; ++i) { - if (cnt[i]) { - ans = ans * 10 + i; - cnt[i]--; + if (cnt[i] > 0) { + --cnt[i]; + ans = i; break; } } } - for (int i = 0; i < 10; ++i) - while (cnt[i]--) ans = ans * 10 + i; + for (int i = 0; i < 10; ++i) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + --cnt[i]; + } + } return ans; } }; @@ -174,46 +188,188 @@ public: #### Go ```go -func smallestNumber(num int64) int64 { - if num == 0 { - return 0 - } - cnt := make([]int, 10) +func smallestNumber(num int64) (ans int64) { neg := num < 0 - if neg { - num = -num - } - for num != 0 { + num = max(num, -num) + cnt := make([]int, 10) + + for num > 0 { cnt[num%10]++ num /= 10 } - ans := 0 + if neg { for i := 9; i >= 0; i-- { - for j := 0; j < cnt[i]; j++ { - ans = ans*10 + i + for cnt[i] > 0 { + ans = ans*10 + int64(i) + cnt[i]-- } } - return -int64(ans) + return -ans } + if cnt[0] > 0 { for i := 1; i < 10; i++ { if cnt[i] > 0 { - ans = ans*10 + i cnt[i]-- + ans = int64(i) break } } } + for i := 0; i < 10; i++ { - for j := 0; j < cnt[i]; j++ { - ans = ans*10 + i + for cnt[i] > 0 { + ans = ans*10 + int64(i) + cnt[i]-- } } - return int64(ans) + + return ans +} +``` + +#### TypeScript + +```ts +function smallestNumber(num: number): number { + const neg = num < 0; + num = Math.abs(num); + const cnt = Array(10).fill(0); + + while (num > 0) { + cnt[num % 10]++; + num = Math.floor(num / 10); + } + + let ans = 0; + if (neg) { + for (let i = 9; i >= 0; i--) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + return -ans; + } + + if (cnt[0] > 0) { + for (let i = 1; i < 10; i++) { + if (cnt[i] > 0) { + cnt[i]--; + ans = i; + break; + } + } + } + + for (let i = 0; i < 10; i++) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn smallest_number(num: i64) -> i64 { + let mut neg = num < 0; + let mut num = num.abs(); + let mut cnt = vec![0; 10]; + + while num > 0 { + cnt[(num % 10) as usize] += 1; + num /= 10; + } + + let mut ans = 0; + if neg { + for i in (0..10).rev() { + while cnt[i] > 0 { + ans = ans * 10 + i as i64; + cnt[i] -= 1; + } + } + return -ans; + } + + if cnt[0] > 0 { + for i in 1..10 { + if cnt[i] > 0 { + cnt[i] -= 1; + ans = i as i64; + break; + } + } + } + + for i in 0..10 { + while cnt[i] > 0 { + ans = ans * 10 + i as i64; + cnt[i] -= 1; + } + } + + ans + } } ``` +#### JavaScript + +```js +/** + * @param {number} num + * @return {number} + */ +var smallestNumber = function (num) { + const neg = num < 0; + num = Math.abs(num); + const cnt = Array(10).fill(0); + + while (num > 0) { + cnt[num % 10]++; + num = Math.floor(num / 10); + } + + let ans = 0; + if (neg) { + for (let i = 9; i >= 0; i--) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + return -ans; + } + + if (cnt[0] > 0) { + for (let i = 1; i < 10; i++) { + if (cnt[i] > 0) { + cnt[i]--; + ans = i; + break; + } + } + } + + for (let i = 0; i < 10; i++) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + + return ans; +}; +``` + diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp index 6915407b07e1a..85daf64390f7e 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp @@ -1,31 +1,38 @@ class Solution { public: long long smallestNumber(long long num) { - if (num == 0) return 0; - vector cnt(10); bool neg = num < 0; num = abs(num); - while (num) { - cnt[num % 10]++; + int cnt[10]{}; + while (num > 0) { + ++cnt[num % 10]; num /= 10; } long long ans = 0; if (neg) { - for (int i = 9; i >= 0; --i) - while (cnt[i]--) ans = ans * 10 + i; + for (int i = 9; i >= 0; --i) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + --cnt[i]; + } + } return -ans; } if (cnt[0]) { for (int i = 1; i < 10; ++i) { - if (cnt[i]) { - ans = ans * 10 + i; - cnt[i]--; + if (cnt[i] > 0) { + --cnt[i]; + ans = i; break; } } } - for (int i = 0; i < 10; ++i) - while (cnt[i]--) ans = ans * 10 + i; + for (int i = 0; i < 10; ++i) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + --cnt[i]; + } + } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.go b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.go index 1d114fc100853..c48b991c05dcc 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.go +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.go @@ -1,38 +1,39 @@ -func smallestNumber(num int64) int64 { - if num == 0 { - return 0 - } - cnt := make([]int, 10) +func smallestNumber(num int64) (ans int64) { neg := num < 0 - if neg { - num = -num - } - for num != 0 { + num = max(num, -num) + cnt := make([]int, 10) + + for num > 0 { cnt[num%10]++ num /= 10 } - ans := 0 + if neg { for i := 9; i >= 0; i-- { - for j := 0; j < cnt[i]; j++ { - ans = ans*10 + i + for cnt[i] > 0 { + ans = ans*10 + int64(i) + cnt[i]-- } } - return -int64(ans) + return -ans } + if cnt[0] > 0 { for i := 1; i < 10; i++ { if cnt[i] > 0 { - ans = ans*10 + i cnt[i]-- + ans = int64(i) break } } } + for i := 0; i < 10; i++ { - for j := 0; j < cnt[i]; j++ { - ans = ans*10 + i + for cnt[i] > 0 { + ans = ans*10 + int64(i) + cnt[i]-- } } - return int64(ans) -} \ No newline at end of file + + return ans +} diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.java b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.java index d1ac4c1459101..562f45facab17 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.java +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.java @@ -1,20 +1,18 @@ class Solution { public long smallestNumber(long num) { - if (num == 0) { - return 0; - } - int[] cnt = new int[10]; boolean neg = num < 0; num = Math.abs(num); - while (num != 0) { - cnt[(int) (num % 10)]++; + int[] cnt = new int[10]; + while (num > 0) { + ++cnt[(int) (num % 10)]; num /= 10; } long ans = 0; if (neg) { for (int i = 9; i >= 0; --i) { - while (cnt[i]-- > 0) { + while (cnt[i] > 0) { ans = ans * 10 + i; + --cnt[i]; } } return -ans; @@ -22,17 +20,18 @@ public long smallestNumber(long num) { if (cnt[0] > 0) { for (int i = 1; i < 10; ++i) { if (cnt[i] > 0) { - ans = ans * 10 + i; - cnt[i]--; + --cnt[i]; + ans = i; break; } } } for (int i = 0; i < 10; ++i) { - while (cnt[i]-- > 0) { + while (cnt[i] > 0) { ans = ans * 10 + i; + --cnt[i]; } } return ans; } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.js b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.js new file mode 100644 index 0000000000000..09f925c7fc57d --- /dev/null +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.js @@ -0,0 +1,44 @@ +/** + * @param {number} num + * @return {number} + */ +var smallestNumber = function (num) { + const neg = num < 0; + num = Math.abs(num); + const cnt = Array(10).fill(0); + + while (num > 0) { + cnt[num % 10]++; + num = Math.floor(num / 10); + } + + let ans = 0; + if (neg) { + for (let i = 9; i >= 0; i--) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + return -ans; + } + + if (cnt[0] > 0) { + for (let i = 1; i < 10; i++) { + if (cnt[i] > 0) { + cnt[i]--; + ans = i; + break; + } + } + } + + for (let i = 0; i < 10; i++) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + + return ans; +}; diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.py b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.py index 0056a75438559..02c900b39c98d 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.py +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.py @@ -1,26 +1,26 @@ class Solution: def smallestNumber(self, num: int) -> int: - if num == 0: - return 0 - cnt = [0] * 10 neg = num < 0 num = abs(num) + cnt = [0] * 10 while num: - num, v = divmod(num, 10) - cnt[v] += 1 - ans = "" + cnt[num % 10] += 1 + num //= 10 + ans = 0 if neg: - for i in range(9, -1, -1): - if cnt[i]: - ans += str(i) * cnt[i] - return -int(ans) + for i in reversed(range(10)): + for _ in range(cnt[i]): + ans *= 10 + ans += i + return -ans if cnt[0]: for i in range(1, 10): if cnt[i]: - ans += str(i) + ans = i cnt[i] -= 1 break for i in range(10): - if cnt[i]: - ans += str(i) * cnt[i] - return int(ans) + for _ in range(cnt[i]): + ans *= 10 + ans += i + return ans diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.rs b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.rs new file mode 100644 index 0000000000000..c0a908d750182 --- /dev/null +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.rs @@ -0,0 +1,42 @@ +impl Solution { + pub fn smallest_number(num: i64) -> i64 { + let mut neg = num < 0; + let mut num = num.abs(); + let mut cnt = vec![0; 10]; + + while num > 0 { + cnt[(num % 10) as usize] += 1; + num /= 10; + } + + let mut ans = 0; + if neg { + for i in (0..10).rev() { + while cnt[i] > 0 { + ans = ans * 10 + i as i64; + cnt[i] -= 1; + } + } + return -ans; + } + + if cnt[0] > 0 { + for i in 1..10 { + if cnt[i] > 0 { + cnt[i] -= 1; + ans = i as i64; + break; + } + } + } + + for i in 0..10 { + while cnt[i] > 0 { + ans = ans * 10 + i as i64; + cnt[i] -= 1; + } + } + + ans + } +} diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.ts b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.ts new file mode 100644 index 0000000000000..4c9ea2bf55da3 --- /dev/null +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.ts @@ -0,0 +1,40 @@ +function smallestNumber(num: number): number { + const neg = num < 0; + num = Math.abs(num); + const cnt = Array(10).fill(0); + + while (num > 0) { + cnt[num % 10]++; + num = Math.floor(num / 10); + } + + let ans = 0; + if (neg) { + for (let i = 9; i >= 0; i--) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + return -ans; + } + + if (cnt[0] > 0) { + for (let i = 1; i < 10; i++) { + if (cnt[i] > 0) { + cnt[i]--; + ans = i; + break; + } + } + } + + for (let i = 0; i < 10; i++) { + while (cnt[i] > 0) { + ans = ans * 10 + i; + cnt[i]--; + } + } + + return ans; +} diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/README.md b/solution/2100-2199/2169.Count Operations to Obtain Zero/README.md index 1a47df3e9151d..34c7a874ceeeb 100644 --- a/solution/2100-2199/2169.Count Operations to Obtain Zero/README.md +++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/README.md @@ -69,7 +69,17 @@ tags: -### 方法一 +### 方法一:模拟 + +我们可以直接模拟这个过程,循环执行以下操作: + +- 如果 $\textit{num1} \ge \textit{num2}$,则 $\textit{num1} = \textit{num1} - \textit{num2}$; +- 否则,$\textit{num2} = \textit{num2} - \textit{num1}$。 +- 每执行一次操作,操作数加一。 + +当 $\textit{num1}$ 或 $\textit{num2}$ 有一个为 $0$ 时,停止循环,返回操作数。 + +时间复杂度 $O(m)$,其中 $m$ 为 $\textit{num1}$ 和 $\textit{num2}$ 的最大值。空间复杂度 $O(1)$。 @@ -81,8 +91,9 @@ class Solution: ans = 0 while num1 and num2: if num1 >= num2: - num1, num2 = num2, num1 - num2 -= num1 + num1 -= num2 + else: + num2 -= num1 ans += 1 return ans ``` @@ -93,13 +104,12 @@ class Solution: class Solution { public int countOperations(int num1, int num2) { int ans = 0; - while (num1 != 0 && num2 != 0) { + for (; num1 != 0 && num2 != 0; ++ans) { if (num1 >= num2) { num1 -= num2; } else { num2 -= num1; } - ++ans; } return ans; } @@ -113,10 +123,154 @@ class Solution { public: int countOperations(int num1, int num2) { int ans = 0; - while (num1 && num2) { - if (num1 > num2) swap(num1, num2); + for (; num1 && num2; ++ans) { + if (num1 >= num2) { + num1 -= num2; + } else { + num2 -= num1; + } + } + return ans; + } +}; +``` + +#### Go + +```go +func countOperations(num1 int, num2 int) (ans int) { + for ; num1 != 0 && num2 != 0; ans++ { + if num1 >= num2 { + num1 -= num2 + } else { + num2 -= num1 + } + } + return +} +``` + +#### TypeScript + +```ts +function countOperations(num1: number, num2: number): number { + let ans = 0; + for (; num1 && num2; ++ans) { + if (num1 >= num2) { + num1 -= num2; + } else { num2 -= num1; - ++ans; + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 { + let mut ans = 0; + while num1 != 0 && num2 != 0 { + ans += 1; + if num1 >= num2 { + num1 -= num2; + } else { + num2 -= num1; + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} num1 + * @param {number} num2 + * @return {number} + */ +var countOperations = function (num1, num2) { + let ans = 0; + for (; num1 && num2; ++ans) { + if (num1 >= num2) { + num1 -= num2; + } else { + num2 -= num1; + } + } + return ans; +}; +``` + + + + + + + +### 方法二:数学 + +如果按照方法一的模拟过程,我们会发现,如果 $\textit{num1}$ 远大于 $\textit{num2}$,那么每次操作我们都会减少 $\textit{num1}$ 的值,这样会导致操作数过多。我们可以优化这个过程,每次操作时,我们可以直接将 $\textit{num1}$ 除以 $\textit{num2}$ 的商加到答案中,然后将 $\textit{num1}$ 对 $\textit{num2}$ 取余,这样可以减少操作数。 + +时间复杂度 $O(\log m)$,其中 $m$ 为 $\textit{num1}$ 和 $\textit{num2}$ 的最大值。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def countOperations(self, num1: int, num2: int) -> int: + ans = 0 + while num1 and num2: + if num1 >= num2: + ans += num1 // num2 + num1 %= num2 + else: + ans += num2 // num1 + num2 %= num1 + return ans +``` + +#### Java + +```java +class Solution { + public int countOperations(int num1, int num2) { + int ans = 0; + while (num1 != 0 && num2 != 0) { + if (num1 >= num2) { + ans += num1 / num2; + num1 %= num2; + } else { + ans += num2 / num1; + num2 %= num1; + } + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int countOperations(int num1, int num2) { + int ans = 0; + while (num1 && num2) { + if (num1 >= num2) { + ans += num1 / num2; + num1 %= num2; + } else { + ans += num2 / num1; + num2 %= num1; + } } return ans; } @@ -126,16 +280,17 @@ public: #### Go ```go -func countOperations(num1 int, num2 int) int { - ans := 0 +func countOperations(num1 int, num2 int) (ans int) { for num1 != 0 && num2 != 0 { - if num1 > num2 { - num1, num2 = num2, num1 + if num1 >= num2 { + ans += num1 / num2 + num1 %= num2 + } else { + ans += num2 / num1 + num2 %= num1 } - num2 -= num1 - ans++ } - return ans + return } ``` @@ -145,13 +300,61 @@ func countOperations(num1 int, num2 int) int { function countOperations(num1: number, num2: number): number { let ans = 0; while (num1 && num2) { - [num1, num2] = [Math.min(num1, num2), Math.abs(num1 - num2)]; - ans++; + if (num1 >= num2) { + ans += (num1 / num2) | 0; + num1 %= num2; + } else { + ans += (num2 / num1) | 0; + num2 %= num1; + } } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 { + let mut ans = 0; + while num1 != 0 && num2 != 0 { + if num1 >= num2 { + ans += num1 / num2; + num1 %= num2; + } else { + ans += num2 / num1; + num2 %= num1; + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} num1 + * @param {number} num2 + * @return {number} + */ +var countOperations = function (num1, num2) { + let ans = 0; + while (num1 && num2) { + if (num1 >= num2) { + ans += (num1 / num2) | 0; + num1 %= num2; + } else { + ans += (num2 / num1) | 0; + num2 %= num1; + } + } + return ans; +}; +``` + diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md b/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md index 06c6724f6ba2a..a83babb934b00 100644 --- a/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md +++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md @@ -35,7 +35,7 @@ tags:
 Input: num1 = 2, num2 = 3
 Output: 3
-Explanation: 
+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.
@@ -48,7 +48,7 @@ So the total number of operations required is 3.
 
 Input: num1 = 10, num2 = 10
 Output: 1
-Explanation: 
+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.
@@ -67,7 +67,17 @@ So the total number of operations required is 1.
 
 
 
-### Solution 1
+### Solution 1: Simulation
+
+We can directly simulate this process by repeatedly performing the following operations:
+
+-   If $\textit{num1} \ge \textit{num2}$, then $\textit{num1} = \textit{num1} - \textit{num2}$;
+-   Otherwise, $\textit{num2} = \textit{num2} - \textit{num1}$.
+-   Each time an operation is performed, increment the operation count by one.
+
+When either $\textit{num1}$ or $\textit{num2}$ becomes $0$, stop the loop and return the operation count.
+
+The time complexity is $O(m)$, where $m$ is the maximum of $\textit{num1}$ and $\textit{num2}$. The space complexity is $O(1)$.
 
 
 
@@ -79,8 +89,9 @@ class Solution:
         ans = 0
         while num1 and num2:
             if num1 >= num2:
-                num1, num2 = num2, num1
-            num2 -= num1
+                num1 -= num2
+            else:
+                num2 -= num1
             ans += 1
         return ans
 ```
@@ -91,13 +102,12 @@ class Solution:
 class Solution {
     public int countOperations(int num1, int num2) {
         int ans = 0;
-        while (num1 != 0 && num2 != 0) {
+        for (; num1 != 0 && num2 != 0; ++ans) {
             if (num1 >= num2) {
                 num1 -= num2;
             } else {
                 num2 -= num1;
             }
-            ++ans;
         }
         return ans;
     }
@@ -111,10 +121,180 @@ class Solution {
 public:
     int countOperations(int num1, int num2) {
         int ans = 0;
-        while (num1 && num2) {
-            if (num1 > num2) swap(num1, num2);
+        for (; num1 && num2; ++ans) {
+            if (num1 >= num2) {
+                num1 -= num2;
+            } else {
+                num2 -= num1;
+            }
+        }
+        return ans;
+    }
+};
+```
+
+#### Go
+
+```go
+func countOperations(num1 int, num2 int) (ans int) {
+	for ; num1 != 0 && num2 != 0; ans++ {
+		if num1 >= num2 {
+			num1 -= num2
+		} else {
+			num2 -= num1
+		}
+	}
+	return
+}
+```
+
+#### TypeScript
+
+```ts
+function countOperations(num1: number, num2: number): number {
+    let ans = 0;
+    for (; num1 && num2; ++ans) {
+        if (num1 >= num2) {
+            num1 -= num2;
+        } else {
             num2 -= num1;
-            ++ans;
+        }
+    }
+    return ans;
+}
+```
+
+#### Rust
+
+```rust
+impl Solution {
+    pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 {
+        let mut ans = 0;
+        while num1 != 0 && num2 != 0 {
+            ans += 1;
+            if num1 >= num2 {
+                num1 -= num2;
+            } else {
+                num2 -= num1;
+            }
+        }
+        ans
+    }
+}
+```
+
+#### JavaScript
+
+```js
+/**
+ * @param {number} num1
+ * @param {number} num2
+ * @return {number}
+ */
+var countOperations = function (num1, num2) {
+    let ans = 0;
+    for (; num1 && num2; ++ans) {
+        if (num1 >= num2) {
+            num1 -= num2;
+        } else {
+            num2 -= num1;
+        }
+    }
+    return ans;
+};
+```
+
+
+
+
+
+
+
+### Solution 2
+
+
+
+#### Python3
+
+```python
+class Solution:
+    def countOperations(self, num1: int, num2: int) -> int:
+        ans = 0
+        while num1 and num2:
+            if num1 >= num2:
+                ans += num1 // num2
+                num1 %= num2
+            else:
+                ans += num2 // num1
+                num2 %= num1
+        return ans
+```
+
+
+
+
+
+
+
+### Solution 2: Mathematics
+
+Following the simulation process in Solution 1, we notice that if $\textit{num1}$ is much larger than $\textit{num2}$, each operation will only reduce the value of $\textit{num1}$ slightly, leading to an excessive number of operations. We can optimize this process by directly adding the quotient of $\textit{num1}$ divided by $\textit{num2}$ to the answer in each operation, then taking the remainder of $\textit{num1}$ divided by $\textit{num2}$. This reduces the number of operations.
+
+The time complexity is $O(\log m)$, where $m$ is the maximum of $\textit{num1}$ and $\textit{num2}$. The space complexity is $O(1)$.
+
+
+
+#### Python3
+
+```python
+class Solution:
+    def countOperations(self, num1: int, num2: int) -> int:
+        ans = 0
+        while num1 and num2:
+            if num1 >= num2:
+                ans += num1 // num2
+                num1 %= num2
+            else:
+                ans += num2 // num1
+                num2 %= num1
+        return ans
+```
+
+#### Java
+
+```java
+class Solution {
+    public int countOperations(int num1, int num2) {
+        int ans = 0;
+        while (num1 != 0 && num2 != 0) {
+            if (num1 >= num2) {
+                ans += num1 / num2;
+                num1 %= num2;
+            } else {
+                ans += num2 / num1;
+                num2 %= num1;
+            }
+        }
+        return ans;
+    }
+}
+```
+
+#### C++
+
+```cpp
+class Solution {
+public:
+    int countOperations(int num1, int num2) {
+        int ans = 0;
+        while (num1 && num2) {
+            if (num1 >= num2) {
+                ans += num1 / num2;
+                num1 %= num2;
+            } else {
+                ans += num2 / num1;
+                num2 %= num1;
+            }
         }
         return ans;
     }
@@ -124,16 +304,17 @@ public:
 #### Go
 
 ```go
-func countOperations(num1 int, num2 int) int {
-	ans := 0
+func countOperations(num1 int, num2 int) (ans int) {
 	for num1 != 0 && num2 != 0 {
-		if num1 > num2 {
-			num1, num2 = num2, num1
+		if num1 >= num2 {
+			ans += num1 / num2
+			num1 %= num2
+		} else {
+			ans += num2 / num1
+			num2 %= num1
 		}
-		num2 -= num1
-		ans++
 	}
-	return ans
+	return
 }
 ```
 
@@ -143,13 +324,61 @@ func countOperations(num1 int, num2 int) int {
 function countOperations(num1: number, num2: number): number {
     let ans = 0;
     while (num1 && num2) {
-        [num1, num2] = [Math.min(num1, num2), Math.abs(num1 - num2)];
-        ans++;
+        if (num1 >= num2) {
+            ans += (num1 / num2) | 0;
+            num1 %= num2;
+        } else {
+            ans += (num2 / num1) | 0;
+            num2 %= num1;
+        }
     }
     return ans;
 }
 ```
 
+#### Rust
+
+```rust
+impl Solution {
+    pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 {
+        let mut ans = 0;
+        while num1 != 0 && num2 != 0 {
+            if num1 >= num2 {
+                ans += num1 / num2;
+                num1 %= num2;
+            } else {
+                ans += num2 / num1;
+                num2 %= num1;
+            }
+        }
+        ans
+    }
+}
+```
+
+#### JavaScript
+
+```js
+/**
+ * @param {number} num1
+ * @param {number} num2
+ * @return {number}
+ */
+var countOperations = function (num1, num2) {
+    let ans = 0;
+    while (num1 && num2) {
+        if (num1 >= num2) {
+            ans += (num1 / num2) | 0;
+            num1 %= num2;
+        } else {
+            ans += (num2 / num1) | 0;
+            num2 %= num1;
+        }
+    }
+    return ans;
+};
+```
+
 
 
 
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp
index 72f3792646707..878fd013e7928 100644
--- a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp	
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp	
@@ -2,11 +2,13 @@ class Solution {
 public:
     int countOperations(int num1, int num2) {
         int ans = 0;
-        while (num1 && num2) {
-            if (num1 > num2) swap(num1, num2);
-            num2 -= num1;
-            ++ans;
+        for (; num1 && num2; ++ans) {
+            if (num1 >= num2) {
+                num1 -= num2;
+            } else {
+                num2 -= num1;
+            }
         }
         return ans;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.go b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.go
index c6e5da9c14093..b2b7fcb83dbe0 100644
--- a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.go	
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.go	
@@ -1,11 +1,10 @@
-func countOperations(num1 int, num2 int) int {
-	ans := 0
-	for num1 != 0 && num2 != 0 {
-		if num1 > num2 {
-			num1, num2 = num2, num1
+func countOperations(num1 int, num2 int) (ans int) {
+	for ; num1 != 0 && num2 != 0; ans++ {
+		if num1 >= num2 {
+			num1 -= num2
+		} else {
+			num2 -= num1
 		}
-		num2 -= num1
-		ans++
 	}
-	return ans
-}
\ No newline at end of file
+	return
+}
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.java b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.java
index 089b74edc30c8..0a8142bc75e8e 100644
--- a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.java	
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.java	
@@ -1,14 +1,13 @@
 class Solution {
     public int countOperations(int num1, int num2) {
         int ans = 0;
-        while (num1 != 0 && num2 != 0) {
+        for (; num1 != 0 && num2 != 0; ++ans) {
             if (num1 >= num2) {
                 num1 -= num2;
             } else {
                 num2 -= num1;
             }
-            ++ans;
         }
         return ans;
     }
-}
\ No newline at end of file
+}
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.js b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.js
new file mode 100644
index 0000000000000..b4cf32789b4b7
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.js	
@@ -0,0 +1,16 @@
+/**
+ * @param {number} num1
+ * @param {number} num2
+ * @return {number}
+ */
+var countOperations = function (num1, num2) {
+    let ans = 0;
+    for (; num1 && num2; ++ans) {
+        if (num1 >= num2) {
+            num1 -= num2;
+        } else {
+            num2 -= num1;
+        }
+    }
+    return ans;
+};
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.py b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.py
index 6e2fa8052382d..39aa647d6de17 100644
--- a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.py	
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.py	
@@ -3,7 +3,8 @@ def countOperations(self, num1: int, num2: int) -> int:
         ans = 0
         while num1 and num2:
             if num1 >= num2:
-                num1, num2 = num2, num1
-            num2 -= num1
+                num1 -= num2
+            else:
+                num2 -= num1
             ans += 1
         return ans
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.rs b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.rs
new file mode 100644
index 0000000000000..6d12aeec725d4
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.rs	
@@ -0,0 +1,14 @@
+impl Solution {
+    pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 {
+        let mut ans = 0;
+        while num1 != 0 && num2 != 0 {
+            ans += 1;
+            if num1 >= num2 {
+                num1 -= num2;
+            } else {
+                num2 -= num1;
+            }
+        }
+        ans
+    }
+}
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.ts b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.ts
index ae0b468a66138..66659ecec2919 100644
--- a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.ts	
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.ts	
@@ -1,8 +1,11 @@
 function countOperations(num1: number, num2: number): number {
     let ans = 0;
-    while (num1 && num2) {
-        [num1, num2] = [Math.min(num1, num2), Math.abs(num1 - num2)];
-        ans++;
+    for (; num1 && num2; ++ans) {
+        if (num1 >= num2) {
+            num1 -= num2;
+        } else {
+            num2 -= num1;
+        }
     }
     return ans;
 }
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.cpp b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.cpp
new file mode 100644
index 0000000000000..8d7ac13ff44f0
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.cpp	
@@ -0,0 +1,16 @@
+class Solution {
+public:
+    int countOperations(int num1, int num2) {
+        int ans = 0;
+        while (num1 && num2) {
+            if (num1 >= num2) {
+                ans += num1 / num2;
+                num1 %= num2;
+            } else {
+                ans += num2 / num1;
+                num2 %= num1;
+            }
+        }
+        return ans;
+    }
+};
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.go b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.go
new file mode 100644
index 0000000000000..fba2da2f02e26
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.go	
@@ -0,0 +1,12 @@
+func countOperations(num1 int, num2 int) (ans int) {
+	for num1 != 0 && num2 != 0 {
+		if num1 >= num2 {
+			ans += num1 / num2
+			num1 %= num2
+		} else {
+			ans += num2 / num1
+			num2 %= num1
+		}
+	}
+	return
+}
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.java b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.java
new file mode 100644
index 0000000000000..1f016a3b56e76
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.java	
@@ -0,0 +1,15 @@
+class Solution {
+    public int countOperations(int num1, int num2) {
+        int ans = 0;
+        while (num1 != 0 && num2 != 0) {
+            if (num1 >= num2) {
+                ans += num1 / num2;
+                num1 %= num2;
+            } else {
+                ans += num2 / num1;
+                num2 %= num1;
+            }
+        }
+        return ans;
+    }
+}
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.js b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.js
new file mode 100644
index 0000000000000..b708c55d9c76b
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.js	
@@ -0,0 +1,18 @@
+/**
+ * @param {number} num1
+ * @param {number} num2
+ * @return {number}
+ */
+var countOperations = function (num1, num2) {
+    let ans = 0;
+    while (num1 && num2) {
+        if (num1 >= num2) {
+            ans += (num1 / num2) | 0;
+            num1 %= num2;
+        } else {
+            ans += (num2 / num1) | 0;
+            num2 %= num1;
+        }
+    }
+    return ans;
+};
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.py b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.py
new file mode 100644
index 0000000000000..a47931cc5ffb7
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.py	
@@ -0,0 +1,11 @@
+class Solution:
+    def countOperations(self, num1: int, num2: int) -> int:
+        ans = 0
+        while num1 and num2:
+            if num1 >= num2:
+                ans += num1 // num2
+                num1 %= num2
+            else:
+                ans += num2 // num1
+                num2 %= num1
+        return ans
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.rs b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.rs
new file mode 100644
index 0000000000000..d10161555dbb7
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.rs	
@@ -0,0 +1,15 @@
+impl Solution {
+    pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 {
+        let mut ans = 0;
+        while num1 != 0 && num2 != 0 {
+            if num1 >= num2 {
+                ans += num1 / num2;
+                num1 %= num2;
+            } else {
+                ans += num2 / num1;
+                num2 %= num1;
+            }
+        }
+        ans
+    }
+}
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.ts b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.ts
new file mode 100644
index 0000000000000..d3aa16d7a92ee
--- /dev/null
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution2.ts	
@@ -0,0 +1,13 @@
+function countOperations(num1: number, num2: number): number {
+    let ans = 0;
+    while (num1 && num2) {
+        if (num1 >= num2) {
+            ans += (num1 / num2) | 0;
+            num1 %= num2;
+        } else {
+            ans += (num2 / num1) | 0;
+            num2 %= num1;
+        }
+    }
+    return ans;
+}