LeetCode 280. Wiggle Sort C#
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#的更多相关文章
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
- LeetCode 280. Wiggle Sort (摆动排序)$
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] 280. Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- Leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] 324. Wiggle Sort II 摆动排序 II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- 【LeetCode】280. Wiggle Sort 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...
- 【leetcode】280.Wiggle Sort
原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...
- 280. Wiggle Sort
题目: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] ...
- leetcode 324 Wiggle Sort 2
利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数 那么我们是不是只要一左一右配合着插入,就保证了差值+- ...
随机推荐
- error:“Unexpected namespace prefix "xmlns" found for tag LinearLayout”
错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法 androidUnexpected ...
- PHP之语言基础01 By ACReaper
1.PHP中的变量是不需要声明类型的,由$标识变量,变量的命名规则也是字母或者下划线开头,接着任意字符或者下划线. $PI = 3.14; $radius = 5; $cir = $PI * 2 * ...
- IOS设计模式学习(7)单例
1 前言 数学与逻辑学中,singleton定义为“有且仅有一个元素的集合”.因此不管袋子有多大,每次从里面取出弹子的时候,拿到的都是同一个. 2 详述 2.1 简述 面向对象应用程序中的单例类(si ...
- 高反差保留滤镜学习OpenCV:滤镜系列(11)——高反差保留
这几周笔者几篇文章介绍了改高反差保留滤镜的文章. 关联文章的地址 高反差保留就是高通滤波 r=(pix[x,y]-avg(R))/128 pix[x,y]*r+128*(1-r) #include & ...
- HTML5 智能表单
HTML5 智能表单 1.表单新增属性 ☀ autofocus 属性 <input type="text" autofocus/>设置 autofocus 属性,使文 ...
- C#开发学习——SqlHelper的应用
使用App.config配置文件封装连接字符串,方便重复使用--->添加App.conifg配置文件--->Add : ConnectionString:--->添加引用 <? ...
- less基本语法
1.新建less文件 xx.less 和css文件存放在一起 2. 在less文件里声明编码格式 @charset "utf-8"; 3.把项目拖入Koala里 4.选中less文 ...
- CentOS7 install vsftpd
#mkdir -p /var/ftp/xcl/ #yum install -y vsftpd#useradd -g ftp -M -d /var/ftp/xcl -s /sbin/nologin xc ...
- Windows下载地址
文件名 cn_windows_7_professional_with_sp1_x64_dvd_u_677031.iso SHA1 9B57E67888434C24DD683968A3CE2C72755 ...
- jndi 与 jdbc
现在开发中经常用到数据库的两种配置1 jdbc2 jndi 一般开发环境都会使用jdbc环境,自己弄配置方便.但是测试和生产环境一般都使用jndi方式.原因有:1 使用jndi方式配置,数据库的 ...