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

[Swift]LeetCode324. 摆动排序 II | Wiggle Sort II

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

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

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

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

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....

Example 1:

Input: nums = [1, 5, 1, 1, 6, 4]
Output: One possible answer is [1, 4, 1, 5, 1, 6].

Example 2:

Input: nums = [1, 3, 2, 2, 3, 1]
Output: One possible answer is [2, 3, 1, 3, 1, 2].

Note:
You may assume all input has valid answer.

Follow Up:
Can you do it in O(n) time and/or in-place with O(1) extra space?


给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序。

示例 1:

输入: nums = [1, 5, 1, 1, 6, 4]
输出: 一个可能的答案是 [1, 4, 1, 5, 1, 6]

示例 2:

输入: nums = [1, 3, 2, 2, 3, 1]
输出: 一个可能的答案是 [2, 3, 1, 3, 1, 2]

说明:
你可以假设所有输入都会得到有效的结果。

进阶:
你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?


380ms

 1 class Solution {
 2     func wiggleSort(_ nums: inout [Int]) {
 3         
 4         if nums.count < 2 {
 5             return
 6         }
 7         
 8         var count = nums.count
 9         
10         var begin = 0, end = count - 1, mid = (begin + end) >> 1
11         
12         func patition(_ begin : Int, _ end : Int) -> Int {
13             let index = nums[begin]
14             var low = begin , high = end
15             while low < high {
16                 while low < high && nums[high] >= index {
17                     high -= 1
18                 }
19                 if low < high {
20                     nums[low] = nums[high]
21                     low += 1
22                 }
23                 
24                 while low < high && nums[low] <= index {
25                     low += 1
26                 }
27                 
28                 if low < high {
29                     nums[high] = nums[low]
30                     high -= 1
31                 }
32                 
33             }
34             
35             nums[low] = index
36             
37             return low
38         }
39         
40         var index = patition(begin, end)
41         
42         while index != mid {
43             if index < mid {
44                 begin = index + 1
45             }else {
46                 end = index - 1
47             }
48             
49             index = patition(begin, end)
50         }
51         
52         mid = nums[index]
53         
54         var n = count
55         
56         func A(_ i : Int) -> Int {
57             return (1+2*i)%(count|1)
58         }
59         var i = 0, j = 0, k = count - 1
60         while j <= k {
61             if nums[A(j)] > mid {
62                 nums.swapAt(A(i), A(j))
63                 i+=1
64                 j+=1
65             }else if nums[A(j)] < mid {
66                 nums.swapAt(A(j), A(k))
67                 k-=1
68             }else {
69                 j+=1
70             }
71         }
72     }
73 }

 

posted @ 2019-01-12 19:22  为敢技术  阅读(315)  评论(0编辑  收藏  举报