From 01c2e3efc1a8f49fd7312e8bf52692505a5f7157 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 19 Nov 2025 23:08:32 +0800 Subject: [PATCH] Add solution and test-cases for problem 2154 --- .../README.md | 36 +++++++++++-------- .../Solution.go | 20 +++++++++-- .../Solution_test.go | 22 ++++++------ 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/README.md b/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/README.md index e747d1563..d4b4fecb4 100755 --- a/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/README.md +++ b/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/README.md @@ -1,28 +1,36 @@ # [2154.Keep Multiplying Found Values by Two][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 an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`. + +You then do the following steps: + +1. If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`). +2. Otherwise, **stop** the process. +3. **Repeat** this process with the new number as long as you keep finding the number. + +Return the **final** value of `original`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [5,3,6,1,12], original = 3 +Output: 24 +Explanation: +- 3 is found in nums. 3 is multiplied by 2 to obtain 6. +- 6 is found in nums. 6 is multiplied by 2 to obtain 12. +- 12 is found in nums. 12 is multiplied by 2 to obtain 24. +- 24 is not found in nums. Thus, 24 is returned. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Keep Multiplying Found Values by Two -```go ``` - +Input: nums = [2,7,9], original = 4 +Output: 4 +Explanation: +- 4 is not found in nums. Thus, 4 is returned. +``` ## 结语 diff --git a/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution.go b/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution.go index d115ccf5e..1eb853ef1 100644 --- a/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution.go +++ b/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution.go @@ -1,5 +1,21 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, original int) int { + var ( + ret int = original + ok bool = false + end int = 0 + ) + m := make(map[int]struct{}) + for _, n := range nums { + end = max(end, n) + m[n] = struct{}{} + } + for ; ret <= end; ret *= 2 { + _, ok = m[ret] + if !ok { + break + } + } + return ret } diff --git a/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution_test.go b/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution_test.go index 14ff50eb4..2906ec07c 100644 --- a/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution_test.go +++ b/leetcode/2101-2200/2154.Keep-Multiplying-Found-Values-by-Two/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + inputs []int + original int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{5, 3, 6, 1, 12}, 3, 24}, + {"TestCase2", []int{2, 7, 9}, 4, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.original) 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.inputs, c.original) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }