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 ...
随机推荐
- git使用-忽略文件更新的几种方法
有几种情况我们不希望本地文件在 git 里面得到更新. 一.情况:始终不需要git跟踪本地的一些文件 方法:使用.gitignore文件忽略 解释: 使用git init操作创建git控制管理之后,默 ...
- python入门及数字、字符串类型
目录 python开发框架 开发 1. 开发语言 2. 语言比对 3. python安装 4. Python开发IDE:pycharm ,eclipse python入门 1. 第一句Python 2 ...
- Android广播时间——实现强制下线功能
目录 思路:强制下线功能需要先关闭掉所有的活动,然后回到登录界面. 步骤 1.关闭所有活动 2.创建BaseActivity类作为所有活动的父类,因为需要用ActivityCollector管理所有活 ...
- python3语法学习第五天--函数(1)
函数:函数能提高应用的模块性,和代码的重复利用率,是一段可重复使用的代码块 自定义函数: 1.函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 2.任何传入参数和自变量必须放在圆括 ...
- vue 下拉刷新实现
[手动实现下拉刷新]可以用vue-pull-refash 插件代替 //下拉刷新 let scroll = this.$ref.scroll // 获取当前要拖拽的元素 let top = scrol ...
- C# 获取从1月至12月的月初时间和月末时间
public IActionResult GetMonthData() { var dataList = new List<object>(); var currentMonth = Da ...
- 一文解读C# 动态拦截第三方进程中的方法函数(外挂必备)
一.前言 由于项目需要,最近研究了一下跨进程通讯改写第三方程序中的方法(运行中),把自己程序中的目标方法直接覆盖第三方程序中的方法函数:一直没有头绪,通过搜索引擎找了一大堆解决方案,资料甚是稀少,最后 ...
- jQuery的插件和跨域、ajax
1. 插件: 也称组件 什么是: 拥有专属的HTML,CSS和js的独立页面区域 为什么: 重用! 何时: 只要一个功能/区域可能被反复使用时 如何: 3个来源: 1. 官方插件:jquery ui ...
- Angular的面试题
1.Aangular中组件之间通信的方式 答案:Props down 1.调用子组件,通过自定义属性传值 2.子组件内部通过Input来接收属性的值 Events up 1.在父组件中定义一个有参数 ...
- mysql操作之二:fetchone与获取lastrowid
import mySQLdb conn = mySQLdb.connect(host='127.0.0.1',user='root',passwd='123456')cur = conn.cursor ...