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. 第一个windows 小游戏 贪吃蛇

    最近用dx尝试做了一个小的贪吃蛇游戏,代码放到github上面:https://github.com/nightwolf-chen/MyFreakout 说一下自己实现的过程: 首先,我把蛇这个抽象成 ...

  2. linux memory release commands内存清理/释放命令

    linux 内存清理/释放命令 You could find reference from here: http://jingyan.baidu.com/article/597a06436a687f3 ...

  3. lucene 从2.4.0—3.6.0—4.3.1版本升级

    一.从2.4升级到3.6 替换原因:由于使用IBM的jdk导致了查询出现不稳定现象,原因无法找到,只好升级版本,毕竟版本很低 1)替换中文分词器,由原来的MMAnaylze替换为IKAnaylze 2 ...

  4. Fedora 19的U盘安装 以及简单配置

    一.Fedora19的U盘安装 2013年7月3日,Fedora 19正式版发布.Fedora 19除了GNOME桌面版之外,还提供了KDE定制版.LXDE定制版等,有兴趣的网友可以在其官网上下载试用 ...

  5. 维吉尼亚密码java代码实现根据密钥长度计算IC值过程

    package cn.longxuzi; import java.util.Scanner; import org.junit.Test; public class ICUtils { /** * @ ...

  6. Js-Html 前端系列--显示有格式的文本

    var dp = $("#dp").val(); var dpXSS = filterXss(dp); document.getElementById("descript ...

  7. 将WebApi Host到控制台和IIS

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

  8. PHP集成环境自定义设置PHP版本,同时运行多个php版本,700个PHP版本随时切换,一键开启常用模块。

    本文采用我自己开发的纯绿色版WAMP环境(我将这个WAMP环境命名为PHPWAMP) (PHPWAMP默认集成VC,不需要单独安装) 那么什么是WAMP环境?WAMP这个词是什么意思? Windows ...

  9. Docker集群实验环境布署--swarm【7 让docker客户端支持docker-compose】

    Docker-Compose是一个部署多个容器的简单但是非常必要的工具.   登录Docker客户端的服务器(默认是安装了docker-engine的服务器),再安装compose插件 # yum i ...

  10. Docker集群实验环境布署--swarm【2 搭建本地镜像仓库】

      在10.40.100.148上   # docker run -d -p 5000:5000 --restart=always --name docker-registry.venic.com - ...