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.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 }

public class Solution {

public void wiggleSort(int[] nums) {

for(int i=0;i<nums.length-1;i++){

if(i%2==0){

if(nums[i]>nums[i+1]) swap(nums,i,i+1);

}else{

if(nums[i]<nums[i+1]) swap(nums,i,i+1);

}

}

}

public void swap(int[] nums,int i,int j){

int temp = nums[i];

nums[i] = nums[j];

nums[j] = temp;

}

}

时间: 2024-10-06 13:39:58

280. Wiggle Sort的相关文章

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

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

Wiggle Sort I &amp; II

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Notice Please complete the problem in-place. Example Given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 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 Wiggle Sort II

原题链接在这里:https://leetcode.com/problems/wiggle-sort-ii/ Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example:(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. (2) Given n

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