Skip to content

Commit 845e868

Browse files
author
openset
committed
Add: Valid Perfect Square
1 parent baa7803 commit 845e868

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

internal/leetcode/problems_status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ var problemStatus = map[int]bool{
100100
344: true,
101101
345: true,
102102
350: true,
103+
367: true,
103104
371: true,
104105
383: true,
105106
387: true,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
package problem367
2+
3+
func isPerfectSquare(num int) bool {
4+
for i := 1; num > 0; i += 2 {
5+
num -= i
6+
}
7+
return num == 0
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package problem367
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in int
7+
want bool
8+
}
9+
10+
func TestIsPerfectSquare(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: 16,
14+
want: true,
15+
},
16+
{
17+
in: 14,
18+
want: false,
19+
},
20+
{
21+
in: 0,
22+
want: true,
23+
},
24+
{
25+
in: 1,
26+
want: true,
27+
},
28+
{
29+
in: 3,
30+
want: false,
31+
},
32+
}
33+
for _, tt := range tests {
34+
got := isPerfectSquare(tt.in)
35+
if got != tt.want {
36+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)