-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
49 lines (42 loc) · 932 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Foundation
/**
69. Sqrt(x)
Tags: Math、Binary Search
https://leetcode.com/problems/sqrtx/description/
*/
/**
二分查找实现, 20ms
思路: 注意相加相乘时Int溢出即可
*/
class SolutionBinarySearch {
func mySqrt(_ x: Int) -> Int {
if x == 0 {
return 0
}
var low = 1
var high = Int.max
while low <= high {
let mid = low + (high - low) / 2 // 防溢出
if mid <= x / mid {
let next = mid + 1
if next > x / next {
return mid
} else {
low = mid + 1
}
} else {
high = mid - 1
}
}
return -1
}
}
class Solution {
func mySqrt(_ x: Int) -> Int {
var r = x
while r > x / r {
r =
}
}
}
SolutionBinarySearch().mySqrt(9)