Skip to content

Add solution and test-cases for problem 367 #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions leetcode/301-400/0367.Valid-Perfect-Square/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
10 changes: 8 additions & 2 deletions leetcode/301-400/0367.Valid-Perfect-Square/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 5 additions & 6 deletions leetcode/301-400/0367.Valid-Perfect-Square/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading