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. Git管理修改、撤销修改、删除文件

    什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改. a.管理修改 对于提交修改, ...

  2. flush方法和close方法的区别

    package com.yhqtv.demo05.Writer; import java.io.FileWriter; /* * @author XMKJ yhqtv.com Email:yhqtv@ ...

  3. navicat 远程链接Mysql问题

    mysql服务器需要配置 给远程访问配置权限:注意自己的用户名和密码 使用select host, user, password from mysql.user;查看是否配置好相应的用户和密码 nav ...

  4. 一个数组求其最长递增子序列(LIS)

    一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...

  5. ql的python学习之路-day8

    前言:本节主要学习的是函数的全局变量和局部变量以及递归 一.全局变量和局部变量 定义在函数外并且在函数头部的变量,叫做全局变量,全局变量在整个代码中都生效. 局部变量只在函数里生效,这个函数就叫做这个 ...

  6. 用JetBrains PyCharm 开发工具写一个简单python案例

    import urllib.request import re #解析html的内容 def getHtml(url): page=urllib.request.urlopen(url) html=p ...

  7. .net System.Web.Mail发送邮件 (设置发件人 只显示用户名)

    http://blog.163.com/hao_2468/blog/static/130881568201141251642215/ .net System.Web.Mail发送邮件 2011-05- ...

  8. MySQL(4)— 数据查询

    四.数据查询(DQL) 4-1.查询指定字段 select 完整 语法: select [distinct] 字段名1 [as] [别名],字段名2 [as] [别名] from 表1 [ left ...

  9. poj3764字典树路径最大异或和

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6853   Accepted: 1 ...

  10. afert和b的伪类画三角形

    /* 提示信息 */ .content-tishi{ width: 6.93rem; margin: 0 auto; background: #e9eaea; display: flex; flex- ...