File tree Expand file tree Collapse file tree 7 files changed +90
-3
lines changed
Problem/Easy/301-400/367. Valid Perfect Square Expand file tree Collapse file tree 7 files changed +90
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ -
Original file line number Diff line number Diff line change 1- // logic
2-
3- // explanation
1+ // logic, explanation
42
53// To Code : ? min
64// Execution : ? ms
You canβt perform that action at this time.
0 commit comments