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

For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

给一个没有排序的数组,将其重新排序成nums[0] <= nums[1] >= nums[2] <= nums[3]....的样子,要求in-place。

解法:遍历一遍数组, 如果是奇数位置并且其值比下一个大,则交换其值, 如果是偶数位置并且其值比下一个小, 则交换其值. 时间复杂度是O(N)。注意index和实际的位置差1,所以奇偶相反。

Java:

public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length < 2) return;
for (int i = 1; i < nums.length; i++) {
if ((i % 2 == 0 && nums[i] > nums[i - 1]) || (i % 2 == 1 && nums[i] < nums[i - 1])) {
int tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
}
}
}
}  

Java:

public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
for (int i = 1; i < nums.length; i++) {
if (i % 2 == 1) {
if (nums[i] < nums[i - 1]) {
swap(nums, i);
}
} else {
if (nums[i] > nums[i - 1]) {
swap(nums, i);
}
}
}
} private void swap(int[] nums, int i) {
int tmp = nums[i - 1];
nums[i - 1] = nums[i];
nums[i] = tmp;
}
}  

Python:

# Time:  O(n)
# Space: O(1)
class Solution(object):
def wiggleSort(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
for i in xrange(1, len(nums)):
if ((i % 2) and nums[i - 1] > nums[i]) or \
(not (i % 2) and nums[i - 1] < nums[i]):
# Swap unordered elements.
nums[i - 1], nums[i] = nums[i], nums[i - 1]

C++:

// Time:  O(n)
// Space: O(1)
class Solution {
public:
void wiggleSort(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (((i % 2) && nums[i] < nums[i - 1]) ||
(!(i % 2) && nums[i] > nums[i - 1])) {
// Swap unordered elements.
swap(nums[i], nums[i - 1]);
}
}
}
}; 

C++:

// Time Complexity O(nlgn)
class Solution {
public:
void wiggleSort(vector<int> &nums) {
sort(nums.begin(), nums.end());
if (nums.size() <= 2) return;
for (int i = 2; i < nums.size(); i += 2) {
swap(nums[i], nums[i - 1]);
}
}
};

C++:

// Time Complexity O(n)
class Solution {
public:
void wiggleSort(vector<int> &nums) {
if (nums.size() <= 1) return;
for (int i = 1; i < nums.size(); ++i) {
if ((i % 2 == 1 && nums[i] < nums[i - 1]) || (i % 2 == 0 && nums[i] > nums[i - 1])) {
swap(nums[i], nums[i - 1]);
}
}
}
};

  

类似题目:

[LeetCode] Wiggle Sort II 摆动排序 II

All LeetCode Questions List 题目汇总

[LeetCode] 280. Wiggle Sort 摆动排序的更多相关文章

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

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

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

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

  3. [LeetCode] Wiggle Sort 摆动排序

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

  4. Leetcode 280. Wiggle Sort

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

  5. LeetCode 280. Wiggle Sort C#

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

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

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

  7. [LintCode] Wiggle Sort 扭动排序

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

  8. 【LeetCode】280. Wiggle Sort 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...

  9. 【leetcode】280.Wiggle Sort

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

随机推荐

  1. Caused by SSLError("Can’t connect to HTTPS URL because the SSL module is not available)

    window7系统: 今天刚安装的anaconda(开源的Python包管理器),把原来的python3和python2都给卸载了,结果运行爬虫程序的时候报错: Caused by SSLError( ...

  2. python笔记37-史上最好用的发邮件zmail

    简介 python发邮件之前用的是smtplib,代码太过于复杂,学习成本大,并且很多人学不会.之前专门写过一篇https://www.cnblogs.com/yoyoketang/p/7277259 ...

  3. steam相关插件

    批量激活key:https://greasyfork.org/zh-CN/scripts/32718-steamredeemkeys 批量卖卡:https://github.com/Nuklon/St ...

  4. Spring Cloud 微服务:Eureka+Zuul+Ribbon+Hystrix+SpringConfig实现流程图

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  5. SpringBoot项目下的mvnw与mvnw.cmd

    Maven是一个常用的构建工具,但是Maven的版本和插件的配合并不是那么完美,有时候你不得不切换到一个稍微旧一些的版本,以保证所有东西正常工作. 而Gradle提供了一个Wrapper,可以很好解决 ...

  6. javascript Object and new object() object --构造函数

  7. 7、Python变量流程基础(变量、赋值、格式化、运算符、流程控制、range()函数)

    一.执行Python程序的两种方式 1.交互式 在终端内输入“python3”,然后输入python代码 2.命令行式 在终端内输入“python3 文本文件路径” 二.变量 1.变量的组成 Pyth ...

  8. No Compiler is Provided in this environment Perhaps you are running on JRE rather than a JDK

    问题描述: 在使用Jenkins打包的时候,抛出这样的错,但JDK和Maven都是已经安装,没问题了的.其中Jenkins用的Pipline流水线来部署项目. 问题解决: 在使用Pipline部署项目 ...

  9. Java 15周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  10. django-自定义文件上传存储类

    文件储存API:https://yiyibooks.cn/xx/django_182/ref/files/storage.html 编写自定义存储系统:https://yiyibooks.cn/xx/ ...