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

[Swift]LeetCode153. 寻找旋转排序数组中的最小值 | Find Minimum in Rotated Sorted Array

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

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

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

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

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:

Input: [3,4,5,1,2] 
Output: 1

Example 2:

Input: [4,5,6,7,0,1,2]
Output: 0

假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

请找出其中最小的元素。

你可以假设数组中不存在重复元素。

示例 1:

输入: [3,4,5,1,2]
输出: 1

示例 2:

输入: [4,5,6,7,0,1,2]
输出: 0

12ms
 1 class Solution {
 2     func findMin(_ nums: [Int]) -> Int {
 3         if nums.count == 0 {
 4             return 0
 5         }
 6 
 7         var left = 0
 8         var right = nums.count - 1
 9 
10         while left < right {
11             let middle = left + (right - left) / 2
12             if nums[middle] > nums[right] {
13                 left = middle + 1
14             } else {
15                 right = middle
16             }
17         }
18 
19         return nums[left]
20     }
21 }

16ms

 1 class Solution {
 2     func findMin(_ nums: [Int]) -> Int {
 3         
 4         if nums.count == 1{
 5             return nums[0]
 6         }
 7 
 8             if nums[0] > nums[nums.count-1]{
 9                 for i in 0..<(nums.count-1){
10                     if nums[i] > nums[i+1] {
11                         return nums[i+1]
12                     } 
13                 }
14             }else{
15                 return nums[0]
16             }
17             
18         return -1
19     }
20 }

24ms

 1 class Solution {
 2     
 3     func findMin(_ nums: [Int]) -> Int {
 4         var start = 0
 5         var end = nums.count - 1
 6         
 7         while start < end {
 8             if nums[start] < nums[end] {
 9                 return nums[start]
10             }
11             
12             var mid = (start + end) / 2
13             
14             if nums[mid] >= nums[start] {
15                 start = mid + 1
16             }else {
17                 end = mid
18             }
19         }
20         
21         return nums[start]
22     }
23     
24     func findMin2(_ nums: [Int]) -> Int {
25         for i in 1 ..< nums.count {
26             if nums[i] < nums [i-1]{
27                 return nums[i]
28             }
29         }
30         
31         return nums[0]
32     }
33 }

28ms

 1 class Solution {
 2   func findMin(_ nums: [Int]) -> Int {
 3     var start = 0
 4     var end = nums.count - 1
 5     var gap = (end - start) / 2
 6     while (end - start) > 1 {
 7       if nums[gap] > nums[end] {
 8         start = gap
 9         gap = start + ((end - gap) / 2)
10       }else if nums[gap] < nums[end]{
11         end = gap
12         gap = (end - start) / 2
13       }
14     }
15     return nums[end] > nums[start] ? nums[start] : nums[end]
16   }
17 }

 

posted @ 2018-12-03 17:32  为敢技术  阅读(257)  评论(0编辑  收藏  举报