[Swift]LeetCode162. 尋找峯值 | Find Peak Element

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

A peak element is an element that is greater than its neighbors.git

Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.github

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.數組

You may imagine that nums[-1] = nums[n] = -∞.微信

Example 1:函數

Input: nums = 
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.[1,2,3,1]

Example 2:spa

Input: nums = 1,2,1,3,5,6,4]
Output: 1 or 5 
Explanation: Your function can return either index number 1 where the peak element is 2, 
             or index number 5 where the peak element is 6.
[

Note:code

Your solution should be in logarithmic complexity.htm


峯值元素是指其值大於左右相鄰值的元素。blog

給定一個輸入數組 nums,其中 nums[i] ≠ nums[i+1],找到峯值元素並返回其索引。

數組可能包含多個峯值,在這種狀況下,返回任何一個峯值所在位置便可。

你能夠假設 nums[-1] = nums[n] = -∞

示例 1:

輸入: nums = 
輸出: 2
解釋: 3 是峯值元素,你的函數應該返回其索引 2。[1,2,3,1]

示例 2:

輸入: nums = 1,2,1,3,5,6,4]
輸出: 1 或 5 
解釋: 你的函數能夠返回索引 1,其峯值元素爲 2;
     或者返回索引 5, 其峯值元素爲 6。
[

說明:

你的解法應該是 O(logN) 時間複雜度的。


32ms

 1 class Solution {
 2     func findPeakElement(_ nums: [Int]) -> Int {
 3         var low = 0
 4         var high = nums.count - 1
 5         
 6         while low != high {
 7             let mid = (low + high)/2
 8             
 9             if !nums.indices.contains(mid - 1) {
10                 if nums[mid + 1] < nums[mid] { return mid }
11                 low = mid + 1
12                 continue
13             }
14             
15             if nums[mid - 1] < nums[mid] && nums[mid + 1] < nums[mid] {
16                 return mid
17             } 
18             
19             if nums[mid - 1] < nums[mid] && nums[mid + 1] > nums[mid] {
20                 low = mid + 1
21                 continue
22             }
23             
24             high = mid - 1
25         }
26         
27         return high
28     }
29 }

36ms

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

40ms

 1 class Solution {
 2     func findPeakElement(_ nums: [Int]) -> Int {
 3         if nums.count >= 3 {
 4             for i in Array(1..<nums.count - 1) {
 5                 let left = nums[i - 1]
 6                 let value = nums[i]
 7                 let right = nums[i + 1]
 8 
 9                 if value > left && value > right {
10                     return i
11                 }
12             }
13         }
14         
15         let lower = 0
16         let upper = nums.count - 1
17         
18         return nums[lower] >= nums[upper] ? lower : upper
19     }
20 }

44ms

 1 class Solution {
 2     func findPeakElement(_ nums: [Int]) -> Int {
 3         var low = 0
 4         var high = nums.count - 1
 5         
 6         while low != high {
 7             let mid = (low + high)/2
 8             
 9             if !nums.indices.contains(mid - 1) || nums[mid - 1] < nums[mid] {
10                 if nums[mid + 1] < nums[mid] { return mid }
11                 low = mid + 1
12                 continue
13             }
14             
15             high = mid - 1
16         }
17         
18         return high
19     }
20 }

52ms

 1 class Solution {
 2     func findPeakElement(_ nums: [Int]) -> Int {
 3 var left = 0
 4     var right = nums.count - 1
 5     var mid = 0
 6     
 7     while left < right {
 8         mid = (right - left) / 2 + left
 9         
10         if nums[mid] > nums[mid + 1] {
11             right = mid
12         } else {
13             left = mid + 1
14         }
15     }
16     return left
17     }
18 }
相關文章
相關標籤/搜索