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.

 1 public class Solution {
 2     public void WiggleSort(int[] nums) {
 3         int l = nums.Length;
 4         for(int i=1; i<l;i++)
 5         {
 6             if(i%2==1)
 7             {
 8                 if(nums[i]<nums[i-1])
 9                 {
10                     Swap(nums, i);
11                 }
12             }
13             else
14             {
15                 if(nums[i]>nums[i-1])
16                 {
17                     Swap(nums, i);
18                 }
19             }
20         }
21     }
22     public void Swap(int[] nums, int i)
23     {
24         int temp = nums[i-1];
25         nums[i-1]=nums[i];
26         nums[i] = temp;
27     }
28 }
时间: 2024-07-28 23:12:19

LeetCode 280. Wiggle Sort C#的相关文章

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

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]. 题目标签:Array, Sort 题目给了我们一个nums array, 让我们wiggle sort.

[LeetCode#280] Wiggle Sort

Problem: 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]. Analysis: Once you encounter a new problem,

280. Wiggle Sort/324. Wiggle Sort II

280 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] 280要求的是可以大于等于,其实简单. 方法一: 1. 先排序,得到序列 a0<=a1<=a2&l

leetcode [324]Wiggle Sort II

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6]. Example 2: Input: nums = [1, 3, 2, 2, 3, 1] Output: One po

280. Wiggle Sort

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]. 此题需要把数组序号分成偶数和奇数来区别对待,代码如下: p.p1 { margin: 0.0px 0.0

leetcode 324 Wiggle Sort 2

利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数 那么我们是不是只要一左一右配合着插入,就保证了差值+-+-+-的要求? 由于题目输入限制了,必定存在解,所以此处我们不需要担心如果重复中位数太多,出现一左一右相等的情况 算法步骤: 1)找到中位数 利用lc215 Kth Largest...,将k=(nums.length+1) / 2,可以在o(n)内求出中位数 2)一左一右插入 0 2 4 ..

leetcode笔记:Wiggle Sort

一. 题目描写叙述 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] <=

【LeetCode】排序 sort(共20题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [56]Merge Intervals [57]Insert Interval [75]Sort Colors [147]Insertion Sort List [148]Sort List [164]Maximum Gap [179]Largest Number [242]Valid Anagram [252]Meeting Rooms (2018年11月22日,为

Wiggle Sort II

Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 注意事项 You may assume all input has valid answer. 样例 Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. Given nums