import java.util.Arrays;
/**
* @author xnl
* @Description:
* @date: 2022/6/28 21:55
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {1,5,1,1,6,4};
solution.wiggleSort(nums);
System.out.println(Arrays.toString(nums));
}
public void wiggleSort(int[] nums) {
Arrays.sort(nums);
int len = nums.length;
int[] arr = nums.clone();
int x = (len + 1) / 2;
for (int i = 0, j = x - 1, k = len - 1; i < len; i += 2, j--, k--){
// 把开头的节点和中间的节点进行替换
nums[i] = arr[j];
if (i + 1 < len){
nums[i + 1] = arr[k];
}
}
}
}
力扣:324. 摆动排序 II
最新推荐文章于 2025-12-01 18:34:51 发布
本文介绍了一种名为Wiggle Sort的算法,通过Java实现,将给定的整数数组按照特定规则进行排序。作者通过实例展示了如何使用冒泡思想交换数组中开头和中间节点,使数组呈现出'波浪'般的上升和下降趋势。
847

被折叠的 条评论
为什么被折叠?



