Skip to content

Commit 0b7dc09

Browse files
authored
Merge pull request #1274 from 0xff-dev/367
Add solution and test-cases for problem 367
2 parents 0ac2c24 + 3706646 commit 0b7dc09

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

leetcode/301-400/0367.Valid-Perfect-Square/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# [367.Valid Perfect Square][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
Given a positive integer num, return true if `num` is a perfect square or `false` otherwise.
6+
7+
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.
8+
9+
You must not use any built-in library function, such as `sqrt`.
10+
811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: num = 16
15+
Output: true
16+
Explanation: We return true because 4 * 4 = 16 and 4 is an integer.
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Valid Perfect Square
23-
```go
2421
```
25-
22+
Input: num = 14
23+
Output: false
24+
Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
25+
```
2626

2727
## 结语
2828

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(num int) bool {
6+
l := (1 << 31) - 1
7+
index := sort.Search(l, func(i int) bool {
8+
return i*i >= num
9+
})
10+
return index*index == num
511
}

leetcode/301-400/0367.Valid-Perfect-Square/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 16, true},
17+
{"TestCase2", 14, false},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)