From 6bda3ef91cdd92b56f24c32053d8be5897e7c368 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 9 Nov 2025 12:04:39 +0800 Subject: [PATCH] Add solution and test-cases for problem 2169 --- .../README.md | 38 ++++++++++++------- .../Solution.go | 16 +++++++- .../Solution_test.go | 21 +++++----- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/README.md b/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/README.md index 97a834432..b810169ea 100755 --- a/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/README.md +++ b/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/README.md @@ -1,28 +1,38 @@ # [2169.Count Operations to Obtain Zero][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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: a = "11", b = "1" -Output: "100" +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:** -### 思路1 -> ... -Count Operations to Obtain Zero -```go ``` - +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. +``` ## 结语 diff --git a/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution.go b/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution.go index d115ccf5e..5374791ba 100644 --- a/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution.go +++ b/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution.go @@ -1,5 +1,17 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(num1 int, num2 int) int { + var ret, x int + for num1 > 0 && num2 > 0 { + if num1 > num2 { + x = num1 / num2 + ret += x + num1 -= num2 * x + continue + } + x = num2 / num1 + ret += x + num2 -= num1 * x + } + return ret } diff --git a/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution_test.go b/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution_test.go index 14ff50eb4..4fc36ec4d 100644 --- a/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution_test.go +++ b/leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + num1, num2 int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 2, 3, 3}, + {"TestCase2", 10, 10, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.num1, c.num2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.num1, c.num2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }