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的更多相关文章

  1. Leetcode 324.摆动排序II

    摆动排序II 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums ...

  2. LeetCode——324. 摆动排序 II

    给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums = [1, 5 ...

  3. 324. 摆动排序 II(三路划分算法)

    题目: 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums = [ ...

  4. [LeetCode] 324. Wiggle Sort II 摆动排序 II

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. [Leetcode] 第324题 摆动排序II

    一.题目描述 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums ...

随机推荐

  1. [hdu4622 Reincarnation]后缀数组

    题意:给一个长度为2000的字符串,10000次询问区间[L,R]内的不同子串的个数 思路:对原串的每个前缀求一边后缀数组,询问[L,R]就变成了询问[L,n]了,即求一个后缀里面出现了多少个不同子串 ...

  2. VMware Centos7 NAT 无法上网的解决方法

    问题描述: VMware下CentOS7使用NAT上网方式无法连网 解决方案: 网络设置为DHCP自动获取IP 查看主机(不是虚拟机)的相关服务是否打开,主要是VMware DHCP 和VMware ...

  3. python unittest TestCase间共享数据(全局变量的使用)

    文章目录 1.setupclass里设置self.xxx变量,不同用例之间无法实时共享参数变动 2.setupclass里设置globals().["xxx"]变量,不同用例之间可 ...

  4. vue 自己写组件。

    最近在网上看到很多大神都有写博客的习惯,坚持写博客不但可以为自己的平时的学习做好记录积累 无意之中也学还能帮助到一些其他的朋友所以今天我也注册一个账号记录一下学习的点滴!当然本人能力实在有限写出的文章 ...

  5. Mybatis系列二-快速开发

    mybatis学习系列第二篇 分页 在网页中常常用到,在查询数据库内容并想将其输出的时候,因为有时有多组数据,一页展示过于突兀,所以会用到分页操作. 在sql用limit来分页. 首先是UserMap ...

  6. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  7. vue-cli项目上传到github预览问题

    上传前先npm run build 后git push origin master 问题:chunk无法加载? 原因:在github.io请求chunk时,chunk的url使用的是publicPat ...

  8. Linux下几个与磁盘空间和文件尺寸相关的命令

    大家好,我是良许. 硬盘是计算机非常重要的一个部件,不管是代码,还是 UI .声音.文档,抑或是没人时偷偷看的小视频,都需要保存在硬盘里. 对于很多 Linux 服务器,会进行很多的编译操作.而编译操 ...

  9. Gunicorn+Nginx+Flask项目部署

    安装python3.6 1)前往用户根目录 >: cd ~ 2)下载 或 上传 Python3.6.7 >: wget https://www.python.org/ftp/python/ ...

  10. Flask搭建个人博客网站(1)—项目规划--李渣渣(lizaza.cn)

    Flask搭建个人博客网站(1)—项目规划--李渣渣(lizaza.cn) 发布时间:2020-05-2413次浏览 前言 现在市面上又许多比较成熟的博客平台,例如:CSDN,博客园,新浪博客等!对于 ...