【LeetCode】324.Wiggle Sort II(Medium)解题报告
2018-03-08 15:07
405 查看
【LeetCode】324.Wiggle Sort II(Medium)解题报告
题目地址:https://leetcode.com/problems/wiggle-sort-ii/description/
题目描述:
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, 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?
Solution1:
Solution2:
Date:2018年3月8日
题目地址:https://leetcode.com/problems/wiggle-sort-ii/description/
题目描述:
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, 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?
Solution1:
/* 答案有很多种,只返回一种就可以了 非常非常重要的一道题,一定要弄懂 给两种解法 第一种: 先排序,中位数左边的正序插,右边的倒序插,都是向中间方向 time : O(nlogn) space : O(n) */ class Solution { public void wiggleSort(int[] nums) { Arrays.sort(nums); int n = nums.length; int mid = (n - 1) / 2; int index = 0; int[] temp = new int ; for(int i=0 ; i<=mid ; i++){ temp[index] = nums[mid - i]; if(index + 1 < n){ temp[index + 1] = nums[n - i - 1]; } index += 2; } System.arraycopy(temp,0,nums,0,n); } }
Solution2:
/* 答案有很多种,只返回一种就可以了 非常非常重要的一道题,一定要弄懂 给两种解法 第二种: 很巧妙的一种方法,超级厉害 还是找中位数,但是不用nlogn的排序,借用215题 Kth largest element in an array 然后运用方法一的思想。 大于中位数 : 从左往右 奇数位 小于中位数 : 从右往左 偶数位 中位数最后放 reference : https://leetcode.com/problems/wiggle-sort-ii/discuss77682 Original idx: 0 1 2 3 4 5 Mapped idx: 1 3 5 0 2 4 Array: 13 6 4 5 2 5 5 6 4 13 2 5 r i l median = 5 nums[] = 5 time : O(n) space : O(1) */ class Solution { public void wiggleSort(int[] nums) { int median = findKthLargest(nums, (nums.length + 1) / 2); int n = nums.length; int left = 0, right = n - 1; int index = 0; while (index <= right) { if (nums[newIndex(index, n)] > median) { swap(nums, newIndex(left++, n), newIndex(index++, n)); } else if (nums[newIndex(index, n)] < median) { swap(nums, newIndex(right--, n), newIndex(index, n)); } else { index++; } } } private int newIndex(int index, int n) { return (1 + 2 * index) % (n | 1); } public int findKthLargest(int[] nums, int k) { if (nums == null || nums.length == 0) return 0; int left = 0; int right = nums.length - 1; while (true) { int pos = partition(nums, left, right); if (pos + 1 == k) { return nums[pos]; } else if (pos + 1 > k) { right = pos - 1; } else { left = pos + 1; } } } private int partition(int[] nums, int left, int right) { int pivot = nums[left]; int l = left + 1; int r = right; while (l <= r) { if (nums[l] < pivot && nums[r] > pivot) { swap(nums, l++, r--); } if (nums[l] >= pivot) l++; if (nums[r] <= pivot) r--; } swap(nums, left, r); return r; } private void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }
Date:2018年3月8日
相关文章推荐
- 【LeetCode】229.Majority Element II(Medium)解题报告
- 【LeetCode】253.Meeting Rooms II(Medium)解题报告
- 【LeetCode】280.Wiggle Sort (Medium)(带锁题)解题报告
- 【LeetCode】75.Sort Colors(Medium)解题报告
- 【LeetCode】95.Unique Binary Search Trees II(Medium)解题报告
- 【LeetCode】275.H-Index II(Medium)解题报告
- 【LeetCode】244.Shortest Word Distance II(Medium)解题报告
- [leetcode] 324. Wiggle Sort II 解题报告
- 【LeetCode】113.Path Sum II(Medium)解题报告
- 【LeetCode】117.Populating Next Right Pointers in Each Node II(Medium)解题报告
- 【LeetCode】267.Palindrome Permutation II(Medium)解题报告
- LeetCode—Sort List解题报告
- 【LeetCode】419.Battleships in a Board(Medium)解题报告
- [leetcode] 280. Wiggle Sort 解题报告
- 【LeetCode】156.Binary Tree Upside Down(Medium)(加锁题)解题报告
- 【LeetCode】513.Find Bottom Left Tree Value(Medium)解题报告
- 【LeetCode】285.Inorder Successor in BST(Medium)(带锁题)解题报告
- [Leetcode] 672. Bulb Switcher II 解题报告
- 【LeetCode】560.Subarray Sum Equals K(Medium)解题报告
- [Leetcode] 158. Read N Characters Given Read4 II - Call multiple times 解题报告