Skip to content

Commit 7373687

Browse files
committed
πŸ˜“: 367
1 parent 4d4fc09 commit 7373687

File tree

7 files changed

+90
-3
lines changed

7 files changed

+90
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// build in math func
2+
// a.java ... more better
3+
4+
// To Code : 3 min
5+
// Execution : ? ms
6+
7+
// Time complexity: O(1)
8+
// Space complexity: O(1)
9+
10+
class Solution {
11+
public boolean isPerfectSquare(int num) {
12+
int val = (int) Math.pow(num, 0.5);
13+
return num == val * val;
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// build in math func
2+
3+
// To Code : 3 min
4+
// Execution : ? ms
5+
6+
// Time complexity: O(1)
7+
// Space complexity: O(1)
8+
9+
class Solution {
10+
public boolean isPerfectSquare(int num) {
11+
float val = (float) Math.sqrt(num);
12+
return (int) val == val;
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check if last char is zero
2+
// but don't use it
3+
// just a logic
4+
5+
class Solution {
6+
public boolean isPerfectSquare(int num) {
7+
String val = String.valueOf(Math.sqrt(num));
8+
return val.charAt(val.length() - 1) == '0';
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// binary search
2+
3+
class Solution {
4+
public boolean isPerfectSquare(int num) {
5+
long start = 1;
6+
long end = num;
7+
8+
while (start <= end) {
9+
long mid = start + (end - start) / 2;
10+
long mul = mid * mid;
11+
12+
if (mul == num)
13+
return true;
14+
else if (mul > num)
15+
end = mid - 1;
16+
else
17+
start = mid + 1;
18+
}
19+
20+
return false;
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Newton Method
2+
3+
class Solution {
4+
public boolean isPerfectSquare(int num) {
5+
long x = num;
6+
7+
while (x * x > num) {
8+
x = (x + num / x) >> 1;
9+
}
10+
11+
return x * x == num;
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# πŸ“„: 367. Valid Perfect Square
2+
3+
- https://leetcode.com/study-plan/binary-search/?progress=x31a8njd
4+
5+
# βœ…: Solution
6+
7+
- `https://leetcode.com/problems/valid-perfect-square/solutions/130010/python-4-methods-with-time-testing/`
8+
-
9+
- a ... build in .... int check
10+
- b ... pow ... mul check
11+
- b ... out of the box / but don't use it
12+
-
13+
- c ... binary search
14+
- d ... Newton Method
15+
-

β€Žtemplate/a.javaβ€Ž

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// logic
2-
3-
// explanation
1+
// logic, explanation
42

53
// To Code : ? min
64
// Execution : ? ms

0 commit comments

Comments
Β (0)