<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/a6973a.md for this page in Markdown format</div>
重新排列数组
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (85.25%) | 150 | - |
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。
请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。
示例 1:
输入:nums = [2,5,1,3,4,7], n = 3
输出:[2,3,5,4,1,7]
解释:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]示例 2:
输入:nums = [1,2,3,4,4,3,2,1], n = 4
输出:[1,4,2,3,3,2,4,1]示例 3:
输入:nums = [1,1,2,2], n = 2
输出:[1,2,1,2]提示:
1 <= n <= 500nums.length == 2n1 <= nums[i] <= 10^3
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-09 11:11:35
* @LastEditTime: 2022-11-09 11:40:34
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /leetcode/1470.重新排列数组.ts
*/
/*
* @lc app=leetcode.cn id=1470 lang=typescript
*
* [1470] 重新排列数组
*
* https://leetcode.cn/problems/shuffle-the-array/description/
*
* algorithms
* Easy (85.25%)
* Likes: 150
* Dislikes: 0
* Total Accepted: 95.8K
* Total Submissions: 112.4K
* Testcase Example: '[2,5,1,3,4,7]\n3'
*
* 给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。
*
* 请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。
*
*
*
* 示例 1:
*
* 输入:nums = [2,5,1,3,4,7], n = 3
* 输出:[2,3,5,4,1,7]
* 解释:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]
*
*
* 示例 2:
*
* 输入:nums = [1,2,3,4,4,3,2,1], n = 4
* 输出:[1,4,2,3,3,2,4,1]
*
*
* 示例 3:
*
* 输入:nums = [1,1,2,2], n = 2
* 输出:[1,2,1,2]
*
*
*
*
* 提示:
*
*
* 1 <= n <= 500
* nums.length == 2n
* 1 <= nums[i] <= 10^3
*
*
*/
// @lc code=start
function shuffle(nums: number[], n: number): number[] {
const res: number[] = new Array(2 * n).fill(0);
for (let i = 0; i < n; i++) {
res[2 * i] = nums[i];
res[2 * i + 1] = nums[i + n];
}
return res;
}
// @lc code=end