Skip to content

Commit 1f5e847

Browse files
committed
fd
1 parent 1f0efbf commit 1f5e847

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

solution/src/main/java/com/inuker/solution/WiggleSortII.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,27 @@
1313
public class WiggleSortII {
1414

1515
// 时间复杂度O(nlgn),空间复杂度O(n)
16+
// 先排序,然后从中间分成两半,先取前面一半的末尾,再取后半的末尾,再取前半的倒数第二个,再取后半倒数第二个,依次类似
17+
// 注意当个数为奇数时,中间那个要分到前面一半中
1618
public void wiggleSort2(int[] nums) {
1719
int[] arr = nums.clone();
1820
Arrays.sort(arr);
19-
int n = nums.length, k = (n - 1) / 2, j = n - 1;
20-
for (int i = 0; i < n; i++) {
21-
nums[i] = (i & 1) != 0 ? arr[j--] : arr[k--];
21+
int n = nums.length, j = (n - 1) / 2, k = n - 1;
22+
for (int i = 0; i < nums.length; i++) {
23+
nums[i] = i % 2 == 0 ? arr[j--] : arr[k--];
2224
}
2325
}
2426

2527
/**
26-
* 这个更优解法没看明白
28+
* 这个解法的核心思想如下:
29+
* 用partition法将数组分为三波,左边一波为大于median的记为L,中间一波为median记为M,右边一波为小于median的记为S,每一波内顺序都无所谓
30+
* 将L从左到右依次放在奇数位上,将S从右到左依次放在偶数位上,多余的空填M。
31+
* 最终结果类似如下:
32+
* M L S L S M
33+
* M L S L S
34+
* 只要这样交叉放着就能符合wiggle
35+
* 直观的做法是另外开辟一个空间来放wiggle结果
36+
* 如果要求不开辟空间,则只能用坐标映射了
2737
*/
2838
// 时间复杂度O(n),空间复杂度O(l)
2939
public void wiggleSort(int[] nums) {
@@ -40,8 +50,16 @@ public void wiggleSort(int[] nums) {
4050
}
4151
}
4252

53+
/**
54+
* 将数组分为两半,后面一半的个数大于等于前面的一半
55+
* 先取后面一半的第一个,再取前面一半的第一个
56+
* 再取后面一半的第二个,再取前面一半的第二个
57+
* 依次类推,得到一个映射关系
58+
*/
4359
private int newIndex(int index, int n) {
44-
return (1 + 2 * index) % (n | 1);
60+
int ret = (1 + 2 * index) % (n | 1);
61+
// System.out.println(String.format("newIndex for %d, %d = %d", index, n, ret));
62+
return ret;
4563
}
4664

4765
private void swap(int[] nums, int i, int j) {

test/src/main/java/com/inuker/test/main.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.inuker.solution.BinaryTreeInorderTraversal;
44
import com.inuker.solution.ClosestBinarySearchTreeValueII;
55
import com.inuker.solution.InorderSuccessorInBST;
6+
import com.inuker.solution.WiggleSortII;
67
import com.leetcode.library.Interval;
78
import com.leetcode.library.TreeNode;
89

@@ -22,20 +23,21 @@
2223
public class main {
2324

2425
public static void main(String[] args) {
25-
26-
}
27-
28-
public void wiggleSort(int[] nums) {
29-
for (int i = 0; i < nums.length - 1; i++) {
30-
if ((i % 2 == 0 && nums[i] > nums[i + 1]) || (i % 2 != 0 && nums[i] < nums[i + 1])) {
31-
swap(nums, i, i + 1);
32-
}
26+
int[] arr = new int[] {
27+
6, 13, 5, 4, 5, 2
28+
};
29+
new WiggleSortII().wiggleSort(arr);
30+
for (int n : arr) {
31+
System.out.print(n + " ");
3332
}
3433
}
3534

36-
private void swap(int[] nums, int i, int j) {
37-
int t = nums[i];
38-
nums[i] = nums[j];
39-
nums[j] = t;
35+
public void wiggleSort2(int[] nums) {
36+
int[] arr = nums.clone();
37+
Arrays.sort(arr);
38+
int n = nums.length, j = (n - 1) / 2, k = n - 1;
39+
for (int i = 0; i < nums.length; i++) {
40+
nums[i] = i % 2 == 0 ? arr[j--] : arr[k--];
41+
}
4042
}
4143
}

0 commit comments

Comments
 (0)