当前位置:网站首页>力扣解法汇总324-摆动排序 II
力扣解法汇总324-摆动排序 II
2022-06-29 04:26:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个整数数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序。
你可以假设所有输入数组都可以得到满足题目要求的结果。
示例 1:
输入:nums = [1,5,1,1,6,4]
输出:[1,6,1,5,1,4]
解释:[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
示例 2:
输入:nums = [1,3,2,2,3,1]
输出:[2,3,1,3,1,2]
提示:
1 <= nums.length <= 5 * 104
0 <= nums[i] <= 5000
题目数据保证,对于给定的输入 nums ,总能产生满足题目要求的结果
进阶:你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/wiggle-sort-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这里最简单的方式,排序,然后分为两部分,小的部分和大的部分。 * 然后依次小的取一个,大的取一个,直到取完。
代码:
public class Solution324 {
public void wiggleSort(int[] nums) {
int[] arr = nums.clone();
Arrays.sort(arr);
int n = nums.length;
int j = (n + 1) / 2 - 1;
int k = n - 1;
int i = 0;
while (i < nums.length) {
nums[i++] = arr[j--];
if (i < n) {
nums[i++] = arr[k--];
}
}
System.out.print(nums.length);
}
}边栏推荐
猜你喜欢

Facade pattern

PostgreSQL has a cross database references are not implemented bug

Libuv library overview and comparison of libevent, libev and libuv (Reprint)

Visitor pattern

Blue Bridge Cup DFS (I)

What are the circular statements of MySQL

SQL two columns become multi row filter display

Decorator Pattern

LabVIEW显示Unicode字符

Implementation of thread pool based on variable parameter template
随机推荐
Rapid development project -vscode plug-in
【HackTheBox】dancing(SMB)
Visitor pattern
Libuv library overview and comparison of libevent, libev and libuv (Reprint)
String不同创建方式的区别
Open source demo| you draw and I guess -- make your life more interesting
CTO and programmer were both sentenced because the crawler was out of control!
Emotional changes need to be controlled
MySQL column to row conversion without Union
Cucumber test practice
043. (2.12) what will happen if you become enlightened?
pytorch 读写文件
源代码防泄露技术种类浅析
The great gods take connections from the MySQL connection pool in the open of the rich function. The initialization of the connection pool is 20. If the parallelism of the rich function is 1
1015 德才论
1016 部分A+B
The last week! Summary of pre competition preparation for digital model American Games
Blue Bridge Cup DFS (I)
Mécanisme d'attention du canal de fixation
Iterator pattern