当前位置:网站首页>力扣解法汇总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);
}
}边栏推荐
- Pytorch read / write file
- Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
- I came from a major, so I didn't want to outsource
- Practical part: solving the function conflict between swagger and user-defined parameter parser
- 【HackTheBox】dancing(SMB)
- Redis cache penetration, cache breakdown, cache avalanche
- Actual combat! Another opening method of magic modified swagger and knife4j
- Composite pattern
- Why is the test post a giant pit? The 8-year-old tester told you not to be fooled
- 1019 数字黑洞
猜你喜欢

From zero to one, I will teach you to build a "search by text and map" search service (I)

Visitor pattern

Redis cache penetration, cache breakdown, cache avalanche

【新功能】Ambire 钱包集成了 Metis 网络

Decorator Pattern

Blue Bridge Cup DFS (I)

Command pattern

Rapid development project -vscode plug-in

What are the MySQL database constraint types

Proxy mode (proxy)
随机推荐
How to write MySQL scheduled backup script in Linux
What are the basic usage methods of MySQL
[C language] explain the thread recycling function pthread_ join
[hackthebox] dancing (SMB)
yolox出现 RuntimeError: DataLoader worker (pid(s) 17724, 1364, 18928) exited unexpectedly
Redis cache penetration, cache breakdown, cache avalanche
If you choose the right school, you can enter Huawei as a junior college. I wish I had known
How to solve startup failure due to insufficient MySQL memory
Here comes Wi Fi 7. How strong is it?
Installation and configuration of interrealsense d435i camera driver
Analysis of moudo Network Library
c语言 --- 分支结构
1019 数字黑洞
Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
Webassembly learning - dynamic linking
Anaconda's own Spyder editor starts with an error
MySQL column to row conversion without Union
If I hadn't talked to Ali P7, I wouldn't know I was a mallet
Ansible best practices playbook different context rights raising demo
mysql动态加表只能加小表,加大表跑着跑着任务就不读数据了,有什么解决办法吗