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

[Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray

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

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

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

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

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.

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

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 <=.

给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。

你找到的子数组应是最短的,请输出它的长度。

示例 1:

输入: [2, 6, 4, 8, 10, 9, 15]
输出: 5
解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。

说明 :

  1. 输入的数组长度范围在 [1, 10,000]。
  2. 输入的数组可能包含重复元素 ,所以升序的意思是<=。

32ms

 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 }

 

posted @ 2018-10-25 20:10  为敢技术  阅读(323)  评论(0编辑  收藏  举报