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

Solution:

Loop through, when odd index num should be greater then the previous num;

                       when even index num should be smaller then the previous num;

So if even index num is greater then the previous num then it must be greater then the previous of previous num too, so only swap once.

 public class Solution {
public void WiggleSort(int[] nums) {
int l = nums.Length;
for(int i=; i<l;i++)
{
if(i%==)
{
if(nums[i]<nums[i-])
{
Swap(nums, i);
}
}
else
{
if(nums[i]>nums[i-])
{
Swap(nums, i);
}
}
}
}
public void Swap(int[] nums, int i)
{
int temp = nums[i-];
nums[i-]=nums[i];
nums[i] = temp;
}
}

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] 324. Wiggle Sort II 摆动排序 II

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

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

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

  7. 【leetcode】280.Wiggle Sort

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

  8. 280. Wiggle Sort

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

  9. leetcode 324 Wiggle Sort 2

    利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数 那么我们是不是只要一左一右配合着插入,就保证了差值+- ...

随机推荐

  1. java常用的环境变量配置

    JDK配置 JAVA_HOME: C:\java\jdk1.7.0 CLASSPATH: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; PAT ...

  2. linux nVidia driver 304 319 . installation by hand

    It's so painful to install nVidia driver by hand on linux. If you remove it or you want to upgrade b ...

  3. 移动收入超PC端 盛大文学战略转型初见成效

    随着智能手机和平板电脑的普及,越来越多的互联网服务也开始向移动端拓展,除了传统的互联网服务如搜索.即时通信之外,网络文学这项新兴的互联网业务也没忽视对移动端的布局. 7月9日,中国最大的网络文学出版平 ...

  4. ZOJ 1204 一个集合能组成多少个等式

    Additive equations Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other ...

  5. java布尔值进行and和or逻辑运算原理

    先看看如下代码: public class Test { public static void test() { boolean a = true; boolean b = false; if (a ...

  6. Linux编程之给你的程序开后门

    这里说的"后门"并不是教你做坏事,而是让你做好事,搭建自己的调试工具更好地进行调试开发.我们都知道,当程序发生异常错误时,我们需要定位到错误,有时我们还想,我们在不修改程序的前提下 ...

  7. 将WebApi Host到控制台和IIS

    近期学习WebApi,初步感想是用起来很容易上手,概念上也很好理解,唯一不爽的地方就在于如果在Visual Studio环境里建立Webapi程序,它会自动给创建很多文件夹和文件,其中很多都是用不到的 ...

  8. js 禁止复制粘贴全选

    // 取消右键菜单document.oncontextmenu = function(e){ var t = e || window.event; var elm = t.target || t.sr ...

  9. xtrabackup在线备份主库,搭建slave,使用gtid模式

    mysql:5.6.29xtrabackup:2.2.10master:192.168.166.129slave:192.168.166.131mysql数据目录:/data/mysqlmysql备份 ...

  10. JavaEE XML XSL转换(XSLT)

    XSL转换(XSLT) @author ixenos 定义: XSL转换机制可以指定将XML文档转换为其他格式的规则,例如,txt纯文本.XHTML或其他任何XML格式. 用途: XSLT通常用来将某 ...