[Swift]LeetCode153. 尋找旋轉排序數組中的最小值 | Find Minimum in Rotated Sorted Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ylwwueki-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

Find the minimum element.數組

You may assume no duplicate exists in the array.微信

Example 1:spa

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

Example 2:code

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

假設按照升序排序的數組在預先未知的某個點上進行了旋轉。htm

( 例如,數組 [0,1,2,4,5,6,7] 可能變爲 [4,5,6,7,0,1,2] )。blog

請找出其中最小的元素。排序

你能夠假設數組中不存在重複元素。

示例 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 }
相關文章
相關標籤/搜索