LeetCode 280
题目数组
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....app
Example:ui
Input: nums = [3,5,2,1,6,4]
Output: One possible answer is [3,5,1,6,2,4]this
根据题意咱们能够得知,当i为奇数时,nums[i - 1] <= nums[i], 当i为偶数时,num[i - 1] >= nums[i], 遍历数组,遇到奇数且不知足条件的,两两交换;遇到偶数且不知足条件的,两两交换。由于有这个=号,因此比wiggle sort ii要简单不少。spa
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; } }
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:code
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.ip
解题思路:QuickSelectelement
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]; }
题目:
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; }
题目中还要让只遍历一次数组来求解,那么我须要用双指针来作,分别从原数组的首尾往中心移动。
题目:
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); }