为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode69. x 的平方根 | Sqrt(x)

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697974.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Input: 4
Output: 2

Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since 
             the decimal part is truncated, 2 is returned.

实现 int sqrt(int x) 函数。

计算并返回 x 的平方根,其中 是非负整数。

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

示例 1:

输入: 4
输出: 2

示例 2:

输入: 8
输出: 2
说明: 8 的平方根是 2.82842..., 
     由于返回类型是整数,小数部分将被舍去。

二分法用除法(20ms)
 1 class Solution {
 2     func mySqrt(_ x: Int) -> Int {
 3         //二分法
 4         var low:Int = 1
 5         var high:Int = x
 6         while(low <= high)
 7         {
 8             var mid = (low+high)/2
 9             //乘法会导致溢出,应写成除法
10             if mid == x/mid
11             {
12                 return mid
13             }
14             else
15             {
16                 if mid < x/mid
17                 {
18                     low = mid + 1
19                 }
20                 else
21                 {
22                     high = mid - 1
23                 }
24             }
25         }
26         return low-1  
27     }
28 }

二分法用乘法(24ms),可能导致溢出

 1 class Solution {
 2     func mySqrt(_ x: Int) -> Int {
 3         if x < 2 { return x }
 4         
 5         var low = 1
 6         var high = x / 2
 7         
 8         while (low <= high) {
 9             let mid = low + (high - low) / 2
10             if mid * mid <= x && (mid+1) * (mid+1) > x {
11                 return mid
12             } else if mid * mid <= x && (mid+1) * (mid+1) <= x {
13                 low = mid + 1
14             } else {
15                 high = mid - 1
16             }
17         }
18         
19         return -1
20     }
21 }

牛顿迭代法:20ms

 1 class Solution {
 2     func mySqrt(_ x: Int) -> Int {
 3         //牛顿迭代法
 4         //判断x是否为0
 5         if x == 0 {return 0}     
 6         var low:Double = 1.0
 7         var high:Double = Double(x)
 8         var mid:Double = high/2
 9         var y:Double = (mid>1) ? mid : 1 
10         while(true)
11         {
12             var dy:Double = (y * y + high) / Double(y)/2
13             //处理int类型的取绝对值abs(int i)
14             //处理double类型的取绝对值fabs(double i)
15             //处理float类型的取绝对值fabsf(float i)
16             if fabs(y - dy)<=0.01
17             {
18                 return Int(dy)
19             }
20             else
21             {
22                 y = dy
23             }
24         }
25     }
26 }

12ms

1 class Solution {
2   func mySqrt(_ x: Int) -> Int {
3     return Int(Double(x).squareRoot())
4   }
5 }

16ms

 1 class Solution {
 2     func mySqrt(_ x: Int) -> Int {
 3         
 4         if x <= 1 {
 5             return x
 6         }
 7         
 8         var leftIndex = 1
 9         var rightIndex = x
10         
11         while leftIndex <= rightIndex {
12             let middle = (leftIndex + rightIndex) / 2
13             if middle * middle == x {
14                 return middle
15             } else if middle * middle < x {
16                 leftIndex = middle + 1
17             } else if middle * middle > x {
18                 rightIndex = middle - 1
19             }
20         }
21         
22         return rightIndex
23     }
24 }

28ms

 1 class Solution {
 2     func mySqrt(_ x: Int) -> Int {
 3         guard x > 1 else { return x }
 4         var left = 0
 5         var right = x
 6         while left <= right {
 7             let mid = (left + right) / 2
 8             if mid*mid > x {
 9                 right = mid - 1
10             }
11             else {
12                 left = mid + 1
13             } 
14         }
15         return right
16     }
17 }

 

posted @ 2018-09-25 09:33  为敢技术  阅读(1006)  评论(0编辑  收藏  举报