You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0300-0399/0367.Valid Perfect Square/README_EN.md
+47-72
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,11 @@ tags:
53
53
54
54
<!-- solution:start -->
55
55
56
-
### Solution 1: Binary search
56
+
### Solution 1: Binary Search
57
+
58
+
We can use binary search to solve this problem. Define the left boundary $l = 1$ and the right boundary $r = num$ of the binary search, then find the smallest integer $x$ that satisfies $x^2 \geq num$ in the range $[l, r]$. Finally, if $x^2 = num$, then $num$ is a perfect square.
59
+
60
+
The time complexity is $O(\log n)$, where $n$ is the given number. The space complexity is $O(1)$.
57
61
58
62
<!-- tabs:start -->
59
63
@@ -62,31 +66,25 @@ tags:
62
66
```python
63
67
classSolution:
64
68
defisPerfectSquare(self, num: int) -> bool:
65
-
left, right =1, num
66
-
while left < right:
67
-
mid = (left + right) >>1
68
-
if mid * mid >= num:
69
-
right = mid
70
-
else:
71
-
left = mid +1
72
-
return left * left == num
69
+
l = bisect_left(range(1, num +1), num, key=lambdax: x * x) +1
70
+
return l * l == num
73
71
```
74
72
75
73
#### Java
76
74
77
75
```java
78
76
classSolution {
79
77
publicbooleanisPerfectSquare(intnum) {
80
-
long left=1, right= num;
81
-
while (left<right) {
82
-
long mid = (left+right) >>>1;
83
-
if (mid * mid >= num) {
84
-
right= mid;
78
+
int l=1, r= num;
79
+
while (l<r) {
80
+
int mid = (l+r) >>>1;
81
+
if (1L*mid * mid >= num) {
82
+
r= mid;
85
83
} else {
86
-
left= mid +1;
84
+
l= mid +1;
87
85
}
88
86
}
89
-
returnleft*left== num;
87
+
returnl*l== num;
90
88
}
91
89
}
92
90
```
@@ -97,15 +95,16 @@ class Solution {
97
95
classSolution {
98
96
public:
99
97
bool isPerfectSquare(int num) {
100
-
long left = 1, right = num;
101
-
while (left < right) {
102
-
long mid = left + right >> 1;
103
-
if (mid * mid >= num)
104
-
right = mid;
105
-
else
106
-
left = mid + 1;
98
+
int l = 1, r = num;
99
+
while (l < r) {
100
+
int mid = l + (r - l) / 2;
101
+
if (1LL * mid * mid >= num) {
102
+
r = mid;
103
+
} else {
104
+
l = mid + 1;
105
+
}
107
106
}
108
-
return left * left == num;
107
+
return 1LL * l * l == num;
109
108
}
110
109
};
111
110
```
@@ -114,61 +113,44 @@ public:
114
113
115
114
```go
116
115
func isPerfectSquare(num int) bool {
117
-
left, right := 1, num
118
-
for left < right {
119
-
mid := (left + right) >> 1
120
-
if mid*mid >= num {
121
-
right = mid
122
-
} else {
123
-
left = mid + 1
124
-
}
125
-
}
126
-
return left*left == num
116
+
l := sort.Search(num, func(i int) bool { return i*i >= num })
117
+
return l*l == num
127
118
}
128
119
```
129
120
130
121
#### TypeScript
131
122
132
123
```ts
133
124
function isPerfectSquare(num:number):boolean {
134
-
let left =1;
135
-
let right =num>>1;
136
-
while (left<right) {
137
-
const mid = (left+right) >>>1;
138
-
if (mid*mid<num) {
139
-
left=mid+1;
125
+
let [l, r] = [1, num];
126
+
while (l<r) {
127
+
const mid = (l+r) >>1;
128
+
if (mid>=num/mid) {
129
+
r=mid;
140
130
} else {
141
-
right=mid;
131
+
l=mid+1;
142
132
}
143
133
}
144
-
returnleft*left===num;
134
+
returnl*l===num;
145
135
}
146
136
```
147
137
148
138
#### Rust
149
139
150
140
```rust
151
-
usestd::cmp::Ordering;
152
141
implSolution {
153
142
pubfnis_perfect_square(num:i32) ->bool {
154
-
letnum:i64=numasi64;
155
-
letmutleft=1;
156
-
letmutright=num>>1;
157
-
whileleft<right {
158
-
letmid=left+ (right-left) /2;
159
-
match (mid*mid).cmp(&num) {
160
-
Ordering::Less=> {
161
-
left=mid+1;
162
-
}
163
-
Ordering::Greater=> {
164
-
right=mid-1;
165
-
}
166
-
Ordering::Equal=> {
167
-
returntrue;
168
-
}
143
+
letmutl=1;
144
+
letmutr=numasi64;
145
+
whilel<r {
146
+
letmid= (l+r) /2;
147
+
ifmid*mid>= (numasi64) {
148
+
r=mid;
149
+
} else {
150
+
l=mid+1;
169
151
}
170
152
}
171
-
left*left==num
153
+
l*l==(numasi64)
172
154
}
173
155
}
174
156
```
@@ -179,20 +161,11 @@ impl Solution {
179
161
180
162
<!-- solution:start -->
181
163
182
-
### Solution 2: Math trick
164
+
### Solution 2: Mathematics
183
165
184
-
This is a math problem:
166
+
Since $1 + 3 + 5 + \cdots + (2n - 1) = n^2$, we can gradually subtract $1, 3, 5, \cdots$ from $num$. If $num$ finally equals $0$, then $num$ is a perfect square.
185
167
186
-
```bash
187
-
1 = 1
188
-
4 = 1 + 3
189
-
9 = 1 + 3 + 5
190
-
16 = 1 + 3 + 5 + 7
191
-
25 = 1 + 3 + 5 + 7 + 9
192
-
36 = 1 + 3 + 5 + 7 + 9 + 11
193
-
....
194
-
so 1+3+...+(2n-1) = (2n-1 + 1)n/2 = n²
195
-
```
168
+
The time complexity is $O(\sqrt n)$, and the space complexity is $O(1)$.
0 commit comments