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[0] < nums[1] > nums[2] < nums[3]...,并给了我们例子。我们可以先给数组排序,然后在做调整。调整的方法是找到数组的中间的数,相当于把有序数组从中间分成两部分,然后从前半段的末尾取一个,在从后半的末尾去一个,这样保证了第一个数小于第二个数,然后从前半段取倒数第二个,从后半段取倒数第二个,这保证了第二个数大于第三个数,且第三个数小于第四个数,以此类推直至都取完,参见代码如下:

解法一:

// O(n) space
class Solution {
public:
void wiggleSort(vector<int>& nums) {
vector<int> tmp = nums;
int n = nums.size(), k = (n + ) / , j = n;
sort(tmp.begin(), tmp.end());
for (int i = ; i < n; ++i) {
nums[i] = i & ? tmp[--j] : tmp[--k];
}
}
};

这道题的Follow up让我们用O(n)的时间复杂度和O(1)的空间复杂度,这个真的比较难,参见网友的解答,(未完待续。。)

解法二:

// O(1) space
class Solution {
public:
void wiggleSort(vector<int>& nums) {
#define A(i) nums[(1 + 2 * i) % (n | 1)]
int n = nums.size(), i = , j = , k = n - ;
auto midptr = nums.begin() + n / ;
nth_element(nums.begin(), midptr, nums.end());
int mid = *midptr;
while (j <= k) {
if (A(j) > mid) swap(A(i++), A(j++));
else if (A(j) < mid) swap(A(j), A(k--));
else ++j;
}
}
};

类似题目:

Sort Colors

Kth Largest Element in an Array

Wiggle Sort

参考资料:

https://leetcode.com/problemset/algorithms/

https://leetcode.com/problems/wiggle-sort-ii/discuss/77706/Short-simple-C%2B%2B

https://leetcode.com/problems/wiggle-sort-ii/discuss/77677/O(n)%2BO(1)-after-median-Virtual-Indexing

https://leetcode.com/problems/wiggle-sort-ii/discuss/77682/Step-by-step-explanation-of-index-mapping-in-Java

https://leetcode.com/problems/wiggle-sort-ii/discuss/77681/O(n)-time-O(1)-space-solution-with-detail-explanations

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Wiggle Sort II 摆动排序之二的更多相关文章

  1. [LeetCode] Wiggle Sort II 摆动排序

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

  2. [LeetCode] 324. Wiggle Sort II 摆动排序 II

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

  3. [LintCode] Wiggle Sort II 扭动排序之二

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

  4. 324 Wiggle Sort II 摆动排序 II

    给定一个无序的数组nums,将它重新排列成nums[0] < nums[1] > nums[2] < nums[3]...的顺序.例子:(1) 给定nums = [1, 5, 1, ...

  5. LeetCode 280. Wiggle Sort (摆动排序)$

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  6. Leetcode Wiggle Sort II

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

  7. [LeetCode] Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  8. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

  9. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

随机推荐

  1. PMM 对MYSQL 的监控配制

      系统选择: centos 7.2     关闭防火墙:     systemctl stop firewalld.service     systemctl disable firewalld.s ...

  2. What is the NETStandard.Library metapackage?

    In my last post, I took a quick look at the Microsoft.AspNetCore meta package. One of the libraries ...

  3. 专门为ADO二层升三层的咏南中间件(特种用途)

    专门为ADO二层升三层的咏南中间件(特种用途) 演示下载:链接: https://pan.baidu.com/s/1bulGBIZ6A1nkeErxIrGsGA 密码: 22dk 解压后运行ynmai ...

  4. line-height 设置为 1

    https://stackoverflow.com/questions/1000398/what-is-line-height1   If no unit is supplied e.g. " ...

  5. APP中的图片如何长按可以下载并保存图片到相册出错处理

    https://www.cnblogs.com/interdrp/p/9302164.html 接上文. 如果用户在保存图片过程中,不小心拒绝了相册的访问权限

  6. Windows上的字符转换之CP_ACP和CP_OEMCP

    原文地址:http://blog.sina.com.cn/s/blog_53c1950a010158mw.html Windows API函数MultiByteToWideChar用于多字节编码字符串 ...

  7. WPF双向数据绑定总结

    参考官方:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/data/data-binding-wpf 实例程序:https://files. ...

  8. SpringBoot2.0针对请求参数@RequestBody验证统一拦截

    title: "SpringBoot2.0针对请求参数@RequestBody验证的统一拦截"categories: SpringBoot2.0 Shirotags: Spring ...

  9. go微服务框架go-micro深度学习-目录

    go微服务框架go-micro深度学习(一) 整体架构介绍 go微服务框架go-micro深度学习(二) 入门例子 go微服务框架go-micro深度学习(三) Registry服务的注册和发现 go ...

  10. Effective Java 第三版——83. 明智谨慎地使用延迟初始化

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...