File tree 4 files changed +176
-0
lines changed
4 files changed +176
-0
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
135
135
- [ ** 349.** Intersection of Two Arrays] ( IntersectionOfTwoArrays/intersection_of_two_arrays.dart )
136
136
- [ ** 223.** Rectangle Area] ( RectangleArea/rectangle_area.dart )
137
137
- [ ** 350.** Intersection of Two Arrays II] ( IntersectionOfTwoArraysII/intersection_of_two_arrays_II.dart )
138
+ - [ ** 367.** Valid Perfect Square] ( ValidPerfectSquare/valid_perfect_square.dart )
138
139
139
140
## Reach me via
140
141
Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+
4
+
5
+ -* Valid Perfect Square *-
6
+
7
+
8
+ Given a positive integer num, write a function which returns True if num is a perfect square else False.
9
+
10
+ Follow up: Do not use any built-in library function such as sqrt.
11
+
12
+
13
+
14
+ Example 1:
15
+
16
+ Input: num = 16
17
+ Output: true
18
+ Example 2:
19
+
20
+ Input: num = 14
21
+ Output: false
22
+
23
+
24
+ Constraints:
25
+
26
+ 1 <= num <= 2^31 - 1
27
+
28
+ */
29
+
30
+ import 'dart:math' ;
31
+
32
+ class A {
33
+ // using math
34
+ bool isPerfectSquare (int num ) {
35
+ double n = sqrt (num );
36
+ if (n ~ / n == 1 ) return true ;
37
+ return false ;
38
+ }
39
+ }
40
+
41
+ class B {
42
+ // binary search
43
+ bool isPerfectSquare (int num ) {
44
+ int low = 0 ;
45
+ int high = num ;
46
+ while (low <= high) {
47
+ int mid = low + (high - low) ~ / 2 ;
48
+ if (mid * mid == num ) {
49
+ return true ;
50
+ } else if (mid * mid < num ) {
51
+ low = mid + 1 ;
52
+ } else {
53
+ high = mid - 1 ;
54
+ }
55
+ }
56
+ return false ;
57
+ }
58
+ }
59
+
60
+ class C {
61
+ bool isPerfectSquare (int num ) {
62
+ for (int i = 1 ; num > 0 ; i += 2 ) num -= i;
63
+ return num == 0 ;
64
+ }
65
+ }
66
+
67
+ class D {
68
+ // Using Newton-Raphson method : X+1 = 1/2 [ Xn + a / Xn ]
69
+ bool isPerfectSquare (int num ) {
70
+ int temp = num ;
71
+ while (temp * temp > num ) temp = (temp + num ~ / temp) ~ / 2 ;
72
+ return temp * temp == num ;
73
+ }
74
+ }
75
+
76
+ class E {
77
+ bool isPerfectSquare (int num ) {
78
+ int i = 1 ;
79
+ while (num > 0 ) {
80
+ num -= i;
81
+ i += 2 ; // The 1 + 3 + 5 + 7 + 9 +......pattern.
82
+ }
83
+ return num == 0 ;
84
+ }
85
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ func isPerfectSquare (num int ) bool {
4
+ var low int = 0
5
+ var high int = num
6
+ for low <= high {
7
+ var mid int = low + (high - low )/ 2
8
+ if mid * mid == num {
9
+ return true
10
+ } else if mid * mid < num {
11
+ low = mid + 1
12
+ } else {
13
+ high = mid - 1
14
+ }
15
+ }
16
+ return false
17
+ }
Original file line number Diff line number Diff line change
1
+ # 🔥 Valid Perfect Square 🔥 || 5 Approaches || Simple Fast and Easy || with Explanation
2
+
3
+ ## Solution - 1 Using Math Sqrt
4
+
5
+ ``` dart
6
+ class Solution {
7
+ bool isPerfectSquare(int num) {
8
+ double n = sqrt(num);
9
+ if (n ~/ n == 1) return true;
10
+ return false;
11
+ }
12
+ }
13
+ ```
14
+
15
+ ## Solution - 2 Binary search Algorithm
16
+
17
+ ``` dart
18
+ class Solution {
19
+ bool isPerfectSquare(int num) {
20
+ int low = 0;
21
+ int high = num;
22
+ while (low <= high) {
23
+ int mid = low + (high - low) ~/ 2;
24
+ if (mid * mid == num) {
25
+ return true;
26
+ } else if (mid * mid < num) {
27
+ low = mid + 1;
28
+ } else {
29
+ high = mid - 1;
30
+ }
31
+ }
32
+ return false;
33
+ }
34
+ }
35
+ ```
36
+
37
+ ## Solution - 3
38
+
39
+ ``` dart
40
+ class Solution {
41
+ bool isPerfectSquare(int num) {
42
+ for (int i = 1; num > 0; i += 2) num -= i;
43
+ return num == 0;
44
+ }
45
+ }
46
+ ```
47
+
48
+ ## Solution - 4 Using Newton-Raphson method : X+1 = 1/2 [ Xn + a / Xn ]
49
+
50
+ ``` dart
51
+ class Solution {
52
+ bool isPerfectSquare(int num) {
53
+ int temp = num;
54
+ while (temp * temp > num) temp = (temp + num ~/ temp) ~/ 2;
55
+ return temp * temp == num;
56
+ }
57
+ }
58
+ ```
59
+
60
+ ## Solution - 5
61
+
62
+ ``` dart
63
+ class Solution {
64
+ bool isPerfectSquare(int num) {
65
+ int i = 1;
66
+ while (num > 0) {
67
+ num -= i;
68
+ i += 2; // The 1 + 3 + 5 + 7 + 9 +......pattern.
69
+ }
70
+ return num == 0;
71
+ }
72
+ }
73
+ ```
You can’t perform that action at this time.
0 commit comments