diff --git a/leetcode/301-400/0367.Valid-Perfect-Square/README.md b/leetcode/301-400/0367.Valid-Perfect-Square/README.md index 66610b391..ecf2f6fd7 100644 --- a/leetcode/301-400/0367.Valid-Perfect-Square/README.md +++ b/leetcode/301-400/0367.Valid-Perfect-Square/README.md @@ -1,28 +1,28 @@ # [367.Valid Perfect Square][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 +Given a positive integer num, return true if `num` is a perfect square or `false` otherwise. + +A **perfect square** is an integer that is the square of an integer. In other words, it is the product of some integer with itself. + +You must not use any built-in library function, such as `sqrt`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: num = 16 +Output: true +Explanation: We return true because 4 * 4 = 16 and 4 is an integer. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Valid Perfect Square -```go ``` - +Input: num = 14 +Output: false +Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer. +``` ## 结语 diff --git a/leetcode/301-400/0367.Valid-Perfect-Square/Solution.go b/leetcode/301-400/0367.Valid-Perfect-Square/Solution.go index d115ccf5e..6abd2854f 100644 --- a/leetcode/301-400/0367.Valid-Perfect-Square/Solution.go +++ b/leetcode/301-400/0367.Valid-Perfect-Square/Solution.go @@ -1,5 +1,11 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(num int) bool { + l := (1 << 31) - 1 + index := sort.Search(l, func(i int) bool { + return i*i >= num + }) + return index*index == num } diff --git a/leetcode/301-400/0367.Valid-Perfect-Square/Solution_test.go b/leetcode/301-400/0367.Valid-Perfect-Square/Solution_test.go index 14ff50eb4..0b9aeff6d 100644 --- a/leetcode/301-400/0367.Valid-Perfect-Square/Solution_test.go +++ b/leetcode/301-400/0367.Valid-Perfect-Square/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 16, true}, + {"TestCase2", 14, false}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }