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 possible answer is [2, 3, 1, 3, 1, 2].

Note:
You may assume all input has valid answer.

Follow Up:
Can you do it in O(n) time and/or in-place with O(1) extra space?

题目大意:

将数组变成为nums[0] < nums[1] > nums[2] < nums[3]....这种形式。

解法:

将数组排好顺序,从左到右偶数位填充比中位数大的数字,从右到左奇数位填充比中位数小的数字,剩下的全填充中卫数。

java:

class Solution {
    public void wiggleSort(int[] nums) {
        if (nums.length<=1) return;
        Arrays.sort(nums);
        int len=nums.length,mid=nums[len/2],low=1,high=(len%2==1?len-1:len-2);
        int[] ans=new int[len];
        Arrays.fill(ans,mid);
        for (int i=len-1;i>=0&&nums[i]>mid;i--,low+=2){ ans[low]=nums[i]; }
        for (int i=0;i<len&&nums[i]<mid;i++,high-=2){ ans[high]=nums[i]; }
        for(int i=0;i<len;i++) nums[i]=ans[i];
    }
}

原文地址:https://www.cnblogs.com/xiaobaituyun/p/10905941.html

时间: 2024-11-10 08:18:34

leetcode [324]Wiggle Sort II的相关文章

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

324. 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 nums = [1, 3, 2, 2, 3, 1], one possible answer is [2,

leetcode 324 Wiggle Sort 2

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

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

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,

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

[Swift]LeetCode324. 摆动排序 II | 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