{ "cells": [ { "cell_type": "markdown", "source": [ "Given an unsorted array `nums`, reorder it such that `nums[0] < nums[1] >\n", "nums[2] < nums[3]...`.\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input:nums = [1, 5, 1, 1, 6, 4]\n", " Output: One possible answer is [1, 4, 1, 5, 1, 6].\n", "\n", "**Example 2:**\n", "\n", "\n", "\n", " Input:nums = [1, 3, 2, 2, 3, 1]\n", " Output: One possible answer is [2, 3, 1, 3, 1, 2].\n", "\n", "**Note:**\n", "You may assume all input has valid answer.\n", "\n", "**Follow Up:**\n", "Can you do it in O(n) time and/or in-place with O(1) extra space?" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "wiggle_sort! (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function wiggle_sort!(nums::Vector{Int})\n", " len = length(nums)\n", " md = len รท 2\n", " partialsort!(nums, md)\n", " nums[1], nums[md] = nums[md], nums[1]\n", " i, j = 2, len\n", " iseven(len) && (j -= 1)\n", " while i <= j\n", " nums[i], nums[j] = nums[j], nums[i]\n", " i += 2\n", " j -= 2\n", " end\n", " return nums\n", "end\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }