Wiggle Sort 系列题目

LeetCode 280 Wiggle Sort

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

根据题意我们可以得知,当i为奇数时,nums[i - 1] <= nums[i], 当i为偶数时,num[i - 1] >= nums[i], 遍历数组,遇到奇数且不满足条件的,两两交换;遇到偶数且不满足条件的,两两交换。因为有这个=号,所以比wiggle sort ii要简单很多。

public void wiggleSort(int[] nums) {
        for(int i = 1; i < nums.length; i++) {
            if (i % 2 == 1 && nums[i] < nums[i - 1]) {
                swap(i, i - 1, nums);
            } else if (i% 2 == 0 && nums[i] > nums[i - 1]) {
                swap(i, i - 1, nums);
            }
        }
    }
    private void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

LeetCode 215 Find Kth Largest Element in Array

LeetCode215
题目
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.

解题思路:QuickSelect

public int findKthLargest(int[] nums, int k) {
        //quickselect
        if (nums == null || nums.length == 0) {
            return -1;
        }
        return quickSelect(nums, 0, nums.length - 1, k);  
    }
    private int quickSelect(int[] nums, int start, int end, int k) {
        //递归的出口
        if (start == end) {
            return nums[start];
        }
        int i = start;
        int j = end;
        int pivot = nums[(i + j) /2];
        //partition
        while (i <= j) {
            while (i <= j && nums[i] > pivot) {
                i++;
            }
            while (i <= j && nums[j] < pivot) {
                j--;
            }
            if ( i <= j ) {
                //swap
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                i++;
                j--;
            }
        }
        //target is on the left
        if (start + k - 1 <= j) {
            return quickSelect(nums, start, j, k);
        }
        //target is on the right
        if (start + k - 1 >= i) {
            return quickSelect(nums, i, end, k - (i - start));
        }
        return nums[j + 1];
        
    }

LeetCode 75 Sort Colors

LeetCode75

题目:
Given an array with n objects colored red, white or blue, sort them in-place 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, and blue respectively.

Note: You are not suppose to use the library's sort function for this problem.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Follow up:

A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with a one-pass algorithm using only constant space?

    public void sortColors(int[] nums) {
        //[1,2,0]
        //r    b
        //[1,0,2]
        // r b
        int red = 0, blue = nums.length - 1;
        for(int i = 0; i <= blue; i++) {
            if (nums[i] == 0) {
                swap(red, i, nums);
                red++;
            } else if (nums[i] == 2) {
                swap(blue, i, nums);
                blue--;
                //虽然交换,但i的位置还是有可能是0或者2
                i--;
            }
        }
    }
    public void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

题目中还要让只遍历一次数组来求解,那么我需要用双指针来做,分别从原数组的首尾往中心移动。

  • 定义red指针指向开头位置,blue指针指向末尾位置
  • 从头开始遍历原数组,如果遇到0,则交换该值和red指针指向的值,并将red指针后移一位。若遇到2,则交换该值和blue指针指向的值,并将blue指针前移一位。若遇到1,则继续遍历。

LeetCode 324 Wiggle Sort II

LeetCode324

题目:
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?

Wiggle Sort II 是一道与findKthLargestElement结合的题目。

重点:
先用findKthLargestElement的思路将数组进行partition,让median的左边全都大于median,右边全都小于median。举例来说,[1,4,5,6,1,1] => [5,6,4,1,1,1]

利用virtual indexing的性质,条件为
mapIndex = (1 + 2 * index) % (n | 1)
n 代表数组个数

可以将index这样转换 -
index 0 1 2 3 4 5
mapp 1 3 5 0 2 4

解题思路:
用quickSelect方法找到median
利用sort color的方法,对mapped index所对应的值进行交换。

     public void wiggleSort(int[] nums) {
        int median = findKthLargestElement(nums, nums.length / 2);
        int red = 0;
        int blue = nums.length - 1;
        int n = nums.length;
        int i =0;
        while(i <= blue) {
            if (nums[mapIndex(i, n)] > median) {
                swap(nums, mapIndex(red, n), mapIndex(i, n));
                red++;
                i++;
            } else if (nums[mapIndex(i, n)] < median) {
                swap(nums, mapIndex(blue, n), mapIndex(i, n));
                blue--;
            } else {
                i++;
            }
        }
        return; 
    }
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    private int mapIndex(int index, int n) {
        return (1 + 2 * index) % (n | 1);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值