题目地址:https://leetcode-cn.com/problems/wiggle-sort/

题目描述

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

Example:

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

题目大意

给你一个无序的数组 nums, 将该数字 原地 重排后使得 nums[0] <= nums[1] >= nums[2] <= nums[3]...

解题方法

排序后交换相邻元素

只要给出一种符合要求的结果即可,因此我们可以找出一种最简单易实现的方案就行。

先把所有的数字排序,然后交换相邻的元素就可以实现这种波浪形的数组。举例如下:

输入:[3,5,2,1,6,4]
排序:[1,2,3,4,5,6]
交换:[1,3,2,5,4,6]

C++代码如下:

class Solution {
public:
void wiggleSort(vector<int>& nums) {
const int N = nums.size();
sort(nums.begin(), nums.end());
for (int i = 1; i < N - 1; i += 2) {
swap(nums[i], nums[i + 1]);
}
}
};

日期

2019 年 9 月 22 日 —— 熬夜废掉半条命

【LeetCode】280. Wiggle Sort 解题报告 (C++)的更多相关文章

  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] 280. 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. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【九度OJ】题目1431:Sort 解题报告

    [九度OJ]题目1431:Sort 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1431 题目描述: 给你n个 ...

随机推荐

  1. 用前端表格技术构建医疗SaaS 解决方案

    电子健康档案(Electronic Health Records, EHR)是将患者在所有医疗机构产生的数据(病历.心电图.医疗影像等)以电子化的方式存储,通过在不同的医疗机构之间共享,让患者面对不同 ...

  2. 通信方案软件设计(环形动态申请内存,支持USART+IIC+SPI+CAN协议

    1 <STM32进阶之串口环形缓冲区实现>中讲得比较清楚(链接) 2 amobbs中讲的方法有点复杂,以下是链接和参考源码: 通信方案软件设计(环形动态申请内存,支持USART+IIC+S ...

  3. 容器之分类与各种测试(三)——slist的用法

    slist和forward_list的不同之处在于其所在的库 使用slist需要包含 #include<ext\list> 而使用forward_list则需要包含 #include< ...

  4. oracle 拆分字符串

    WITH t AS (SELECT '1-2-3-4' a FROM dual)SELECT Regexp_Substr(a, '[^-]+', 1, LEVEL) i FROM tCONNECT B ...

  5. java静态方法调用非静态方法

    我们都知道,静态static方法中不能调用非静态non-static方法,准确地说是不能直接调用non-static方法.但是可以通过将一个对象的引用传入static方法中,再去调用该对象的non-s ...

  6. SQL count和sum

    count(1).count(*)与count(列名)的执行区别 count(1) and count(字段) 两者的主要区别是 (1) count(1) 会统计表中的所有的记录数,包含字段为null ...

  7. @Deprecated注解功能

    @Deprecated注解功能 标记不建议使用的方法,但是仍然可以用 当方法有更好的方法替换时,但是此方法还有使用时可以使用该注解

  8. ICCV2021 | 简单有效的长尾视觉识别新方案:蒸馏自监督(SSD)

    ​  前言  本文提出了一种概念上简单但特别有效的长尾视觉识别的多阶段训练方案,称为蒸馏自监督(Self Supervision to Distillation, SSD).在三个长尾识别基准:Ima ...

  9. 【Fastjson】Fastjson反序列化由浅入深

    Fastjson真-简-介 fastjson是由alibaba开发并维护的一个json工具,以其特有的算法,号称最快的json库 fastjson的使用 首先先创一个简单的测试类User public ...

  10. 远程调用RPC

    一.简介 RPC,就是Remote Procedure Call的简称呀,翻译成中文就是远程过程调用. 本地调用,就好比你现在在家里,你要想洗碗,那你直接把碗放进洗碗机,打开洗碗机开关就可以洗了.这就 ...