[Swift]LeetCode581. 最短無序連續子數組 | Shortest Unsorted Continuous Subarray

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

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.git

You need to find the shortest such subarray and output its length.github

Example 1:數組

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

 Note:微信

  1. Then length of the input array is in range [1, 10,000].
  2. The input array may contain duplicates, so ascending order here means <=.

給定一個整數數組,你須要尋找一個連續的子數組,若是對這個子數組進行升序排序,那麼整個數組都會變爲升序排序。this

你找到的子數組應是最短的,請輸出它的長度。spa

示例 1:code

輸入: [2, 6, 4, 8, 10, 9, 15]
輸出: 5
解釋: 你只須要對 [6, 4, 8, 10, 9] 進行升序排序,那麼整個表都會變爲升序排序。

說明 :htm

  1. 輸入的數組長度範圍在 [1, 10,000]。
  2. 輸入的數組可能包含重複元素 ,因此升序的意思是<=。

32msblog

 1 class Solution {
 2     func findUnsortedSubarray(_ nums: [Int]) -> Int {
 3         let count = nums.count
 4         var (left,right)=(0,count-1)
 5         while left<count-1, nums[left]<=nums[left+1] {
 6             left += 1
 7         }
 8         while right>left, nums[right]>=nums[right-1]{
 9             right -= 1
10         }
11         if left == right {
12             return 0
13         }
14         var (mini,maxi) = (nums[left],nums[right])
15         for i in left...right {
16             mini = min(mini, nums[i])
17             maxi = max(maxi, nums[i])
18         }
19         while left>=0,nums[left]>mini {
20             left -= 1
21         }
22         while right<count,nums[right]<maxi {
23             right += 1
24         }
25         return right-left-1
26     }
27 }

36ms

 1 class Solution {
 2     func findUnsortedSubarray(_ nums: [Int]) -> Int {
 3         let n = nums.count
 4         var start = -1
 5         var end = -2
 6         var minNum = nums[n - 1]
 7         var maxNum = nums[0]
 8         
 9         for i in 1..<n {
10             maxNum = max(maxNum, nums[i])
11             minNum = min(minNum, nums[n - 1 - i])
12             if nums[i] < maxNum {
13                 end = i
14             }
15             if nums[n - 1 - i] > minNum {
16                 start = n - 1 - i
17             }
18         }
19         
20         return end - start + 1 
21     }
22 }

48ms

 1 class Solution {
 2     func findUnsortedSubarray(_ nums: [Int]) -> Int {
 3         let length = nums.count
 4         var minNum = nums[length - 1]
 5         var maxNum = nums[0]
 6         var begin = -1
 7         var end = -2
 8         var i = 0
 9         
10         while i < length {
11             maxNum = max(maxNum, nums[i])
12             minNum = min(minNum, nums[length-i-1])
13             
14             if nums[i] < maxNum {
15                 end = i
16             }
17             if nums[length-i-1] > minNum {
18                 begin = length-i-1
19             }
20             i += 1
21         }
22            
23         return end - begin + 1
24     }
25 }

60ms

 1 class Solution {
 2     func findUnsortedSubarray(_ nums: [Int]) -> Int {
 3                 if nums.count == 0 {
 4             return 0
 5         }
 6         
 7         var rightIndex : Int = 0
 8         var max : Int = nums[0]
 9         for index in 1 ..< nums.count {
10             if nums[index] >= max {
11                 max = nums[index]
12             } else {
13                 rightIndex = index
14             }
15         }
16         
17         var leftIndex : Int = 0
18         var min : Int = nums[nums.count - 1]
19         for index in (0 ..< nums.count - 1).reversed() {
20             if nums[index] <= min {
21                 min = nums[index]
22             } else {
23                 leftIndex = index
24             }
25         }
26         
27         return rightIndex != leftIndex ? rightIndex - leftIndex + 1 : 0
28     }
29 }

208ms

 1 class Solution {
 2     func findUnsortedSubarray(_ nums: [Int]) -> Int {
 3         var sorted = nums.sorted()
 4         var start = nums.count
 5         var end = 0
 6         for index in nums.indices {
 7             if sorted[index] != nums[index] {
 8                 start = min(start, index)
 9                 end = max(end, index)
10             }
11         } 
12         return end - start >= 0 ? end - start + 1 : 0
13     }
14 }

320ms

 1 class Solution {
 2     func findUnsortedSubarray(_ nums: [Int]) -> Int {
 3         var arr:[Int] = nums
 4         //升序排序
 5         arr.sort(){$0 < $1}
 6         var i:Int = 0 , j:Int = nums.count - 1
 7         while(nums[i] == arr[i] && i < j)
 8         {
 9             i += 1
10         }        
11         while(nums[j] == arr[j] && i < j)
12         {
13             j -= 1
14         }
15         if i == j
16         {
17             return 0
18         }
19         else
20         {
21             return j - i + 1
22         } 
23     }
24 }
相關文章
相關標籤/搜索