From 19c5232feace7337f8ab0e2e1c511b870172d43f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 29 Oct 2025 09:37:11 +0800 Subject: [PATCH] Add solution and test-cases for problem 3370 --- .../README.md | 41 +++++++++++++------ .../Solution.go | 11 ++++- .../Solution_test.go | 14 +++---- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/README.md b/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/README.md index 5035313d8..f81fef0be 100755 --- a/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/README.md +++ b/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/README.md @@ -1,28 +1,45 @@ # [3370.Smallest Number With All Set Bits][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 a positive number `n`. + +Return the **smallest** number `x` **greater than** or **equal to** `n`, such that the binary representation of `x` contains only set bits **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 5 + +Output: 7 + +Explanation: + +The binary representation of 7 is "111". +``` + +**Example 2:** + ``` +Input: n = 10 + +Output: 15 -## 题意 -> ... +Explanation: + +The binary representation of 15 is "1111". +``` -## 题解 +**Example 3:** -### 思路1 -> ... -Smallest Number With All Set Bits -```go ``` +Input: n = 3 +Output: 3 + +Explanation: + +The binary representation of 3 is "11". +``` ## 结语 diff --git a/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution.go b/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution.go index d115ccf5e..7f64d6657 100644 --- a/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution.go +++ b/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution.go @@ -1,5 +1,12 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) int { + base := 2 + for i := 0; i < 10; i++ { + if base-1 >= n { + return base - 1 + } + base *= 2 + } + return 0 } diff --git a/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution_test.go b/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution_test.go index 14ff50eb4..fffb3b5a6 100644 --- a/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution_test.go +++ b/leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, 7}, + {"TestCase2", 10, 15}, + {"TestCase3", 3, 3}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }