Wiggle SortII

 1 public class Solution {
 2     public void wiggleSort(int[] nums) {
 3         Arrays.sort(nums);
 4
 5         int[] result = new int[nums.length];
 6         int bound = nums.length % 2 == 1 ? nums.length / 2 : nums.length / 2 - 1;
 7         int i = bound;
 8         int j = nums.length - 1;
 9         int index = 0;
10
11         while (i <= bound) {
12             result[index++] = nums[i--];
13             if (j > bound) {
14                 result[index++] = nums[j--];
15             }
16         }
17
18         for (i = 0; i < nums.length; i++) {
19             nums[i] = result[i];
20         }
21     }
22 }

1. Use backward arrangement in case of [4, 5, 5, 6].

时间: 2024-12-28 20:43:18

Wiggle SortII的相关文章

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

Wiggle Sort I &amp; II

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Notice Please complete the problem in-place. Example Given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 分析: 排序,从第三个开

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 &quot;Wiggle Subsequence&quot; !

Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, longest wiggle(up\down) subseq AT number i, you will have O(n^2) solution class Solution { struct Rec { Rec(): mlen_dw(0), mlen_up(0){} Rec(int ldw, i

【Leetcode】376. Wiggle Subsequence

Description: A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fe

376. Wiggle Subsequence

不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 题意为给定数组,获取满足要求的最长子串的长度.要求为前后两个数字差为绝对的正负关系(差为0不满足要求). 例如,[1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) . 7-1=6>0, 4-7=-3<0, 9-4=5>0, 2-9=-7<0, 5-2=3>0. 采用贪心算法先获取两相邻数的差,再进行判

[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 376. Wiggle Subsequence 摆动子序列

原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than t

Wiggle Subsequence

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two