Java实现 LeetCode 324 摆动排序 II
324. 摆动排序 II
给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]… 的顺序。
示例 1:
输入: nums = [1, 5, 1, 1, 6, 4]
输出: 一个可能的答案是 [1, 4, 1, 5, 1, 6]
示例 2:
输入: nums = [1, 3, 2, 2, 3, 1]
输出: 一个可能的答案是 [2, 3, 1, 3, 1, 2]
说明:
你可以假设所有输入都会得到有效的结果。
进阶:
你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?
class Solution {
public void wiggleSort(int[] nums) {
int max = Integer.MIN_VALUE;
for (int num : nums) {
max = Math.max(num, max);
}
int[] tmp = new int[max + 2];
for (int num : nums) {
tmp[num]++;
}
int a = 0, b = 1, i;
for (i = tmp.length - 1; i > 0; i--) {
while (tmp[i] > 0 && b < nums.length) {
nums[b] = i;
b += 2;
tmp[i]--;
}
if (b >= nums.length) {
break;
}
}
while (i >= 0) {
while (tmp[i] > 0 && a < nums.length) {
nums[a] = i;
a += 2;
tmp[i]--;
}
if (a >= nums.length) {
break;
}
if (tmp[i] > 0) {
for (; tmp[i] > 0 && a < nums.length; tmp[i]--) {
nums[a] = i;
a += 2;
}
}
i--;
}
}
}
Java实现 LeetCode 324 摆动排序 II的更多相关文章
- Leetcode 324.摆动排序II
摆动排序II 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums ...
- LeetCode——324. 摆动排序 II
给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums = [1, 5 ...
- 324. 摆动排序 II(三路划分算法)
题目: 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums = [ ...
- [LeetCode] 324. Wiggle Sort II 摆动排序 II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode] 第324题 摆动排序II
一.题目描述 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums ...
随机推荐
- haskell ide - vscode
以windows为例(因为手头只有这个系统,linux系统下类似) 1. 下载安装vscode 2. 安装haskell的管理工具stack,将路径添加到环境变量path 3. windows下安装s ...
- 使用IR2104S搭建的H桥-机器人队比赛经典版(原作者答疑)
原理图地址:http://bbs.ednchina.com/BLOG_ARTICLE_3020313.HTM?click_from=8800020962,4950449047,2014-05-01,E ...
- 修改托管dll文件
众所周知,托管的dll是可以反编译且可以修改的. 可以用ildasm.exe导出IL文件,修改IL文件后,用ilasm编译成DLL 用ildasm.exe导出IL文件 1.获得ildasm.exe ...
- Rx-Volley 自己来封装
自从15年接触了RxJava,对函数式编程越发的喜爱.以前Android项目上网络层都是统一的使用Volley,已经对网络请求的回调,多个回调嵌入各种不爽了,趁着年前任务轻松,赶紧的将Volley封装 ...
- Intellij Idea2018破解教程(激活到2099年)
1.下载IntelliJ IDEA 2018.1.6 链接:https://pan.baidu.com/s/18ZcKiPp3LU5S-la10r5icw 提取码:ghn0 2.安装IntelliJ ...
- Codeforces 1105D(Kilani and the Game,双队列bfs)
AC代码: #include<bits/stdc++.h> #define ll long long #define endl '\n' #define mem(a,b) memset(a ...
- LTC6804读写配置寄存器
一.写配置寄存器步骤及函数封装 写配置寄存器 1.把CSB拉低至低电平: 2.发送WRCFG命令(0x00 0x01)及其PEC(0x3D 0x6E): 3.发送配置寄存器的CFGR0字节,然后继续发 ...
- Django之JSON数据格式
JSON简介: o JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) o JSON 是轻量级的文本数据交换格式 o JSON ...
- JavaScript基础技术总结
javascript的作用 HTML网页运行在浏览器端,与用户没有交互功能,用户访问网页的时候只能看,如果网页没有程序员去更新,永远是一成不变的.JavaScript就是可以让程序运行在网页上,提高客 ...
- 201771010128王玉兰《面向对象程序设计(Java)》第十三周学习总结
第一部分:基础理论知识 1.事件处理基础 事件源(event source):能够产生事件的对象都可 以成为事件源,如文本框.按钮等.一个事件源是一个 能够注册监听器并向监听器发送事件对象的对象. 事 ...