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] <= nums[1] >= nums[2] <= nums[3],要求in-place。

第一种思路是先排序后调整。

依据题目的描写叙述,数组的组合方式有多种。因此能够先将数组进行排序,这时候从第3个元素開始,将第奇数个元素与前面一个偶数位置元素进行交换,如将第3个元素和第2个元素交换、将第5个元素和第4个元素交换。以此类推。

该方法的时间复杂度为:O(NlogN),空间复杂度:O(1)

第二中思路是greedy,使用这样的方法仅仅需遍历一次数组,时间复杂度为:O(N)

相同满足题目的in-place要求。

我们知道,在题目的描写叙述中能够总结出下面规律:

当下标i是奇数时,nums[i] >= nums[i - 1]

当下标i是偶数时。nums[i] <= nums[i - 1]

依据以上规律。仅仅需遍历一遍数组。把不符合的条件的元素交换一下就可以。

三. 演示样例代码

// 先排序后调整
class Solution {
public:
    void wiggleSort(vector<int>& nums) {
        if(nums.size() < 2) return;  

        sort(nums.begin(),nums.end());
        // 将数组中一对一对交换
        for(int i = 2; i < nums.size(); i += 2){
            int tmp = nums[i-1];
            nums[i-1] = nums[i];
            nums[i] = tmp;
        }
    }
};
// 贪心法
class Solution {
public:
    void wiggleSort(vector<int>& nums) {
        if(nums.size() < 2) return;  

        for (int i = 1; i < nums.size(); ++i) {
            if((i % 2 == 1 && nums[i] < nums[i-1]) || (i % 2 == 0 && nums[i] > nums[i-1]))
                swap(nums[i], nums[i-1]);
        }
    }
};

四. 小结

兴许有题目:Wiggle Sort II。

原文地址:https://www.cnblogs.com/llguanli/p/8481775.html

时间: 2024-11-07 14:07:46

leetcode笔记: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,

leetcode笔记:Sort Colors

一. 题目描述 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white

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

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

leetcode 324 Wiggle Sort 2

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

【leetcode刷题笔记】Sort List

Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找到中点并返回的函数findMiddle; 2.归并函数merge; 3.排序函数sortList. 数组的findMiddle函数非常容易实现,链表就有一点tricky了.首先设置两个指针,一个slow初始化为head,一个fast初始化为head.next,然后slow一次走一步,fast一次走两

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al