File tree 2 files changed +41
-0
lines changed
lcof2/剑指 Offer II 072. 求平方根
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -160,6 +160,29 @@ public class Solution {
160
160
}
161
161
```
162
162
163
+ #### Swift
164
+
165
+ ``` swift
166
+ class Solution {
167
+ func mySqrt (_ x : Int ) -> Int {
168
+ if x == 0 {
169
+ return 0
170
+ }
171
+ var left = 0
172
+ var right = x
173
+ while left < right {
174
+ let mid = (left + right + 1 ) / 2
175
+ if mid <= x / mid {
176
+ left = mid
177
+ } else {
178
+ right = mid - 1
179
+ }
180
+ }
181
+ return left
182
+ }
183
+ }
184
+ ```
185
+
163
186
<!-- tabs: end -->
164
187
165
188
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func mySqrt( _ x: Int ) -> Int {
3
+ if x == 0 {
4
+ return 0
5
+ }
6
+ var left = 0
7
+ var right = x
8
+ while left < right {
9
+ let mid = ( left + right + 1 ) / 2
10
+ if mid <= x / mid {
11
+ left = mid
12
+ } else {
13
+ right = mid - 1
14
+ }
15
+ }
16
+ return left
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments