We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 313a70f commit a96c958Copy full SHA for a96c958
Search/Sqrtx.swift
@@ -9,17 +9,15 @@
9
*/
10
11
class Sqrtx {
12
- func mySqrt(x: Int) -> Int {
13
- if x <= 0 {
14
- return x
+ func mySqrt(_ x: Int) -> Int {
+ guard x >= 0 else {
+ return 0
15
}
16
17
- var left = 0
18
- var right = x / 2 + 1
19
- var mid = 0
20
-
+ var left = 0, right = x / 2 + 1
+
21
while left <= right {
22
- mid = (right - left) / 2 + left
+ let mid = (right - left) / 2 + left
23
24
if mid * mid == x {
25
return mid
@@ -32,4 +30,4 @@ class Sqrtx {
32
30
33
31
return right
34
35
-}
+}
0 commit comments