Skip to content

Commit c0fd669

Browse files
committed
✨feat: add 1470
1 parent 04d4291 commit c0fd669

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
| [1455. 检查单词是否为句中其他单词的前缀](https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/solution/by-ac_oier-cfsi/) | 简单 | 🤩🤩🤩🤩 |
153153
| [1460. 通过翻转子数组使两个数组相等](https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/) | [LeetCode 题解链接](https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/solution/by-ac_oier-pv38/) | 简单 | 🤩🤩🤩🤩 |
154154
| [1464. 数组中两元素的最大乘积](https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/solution/by-ac_oier-t5p3/) | 简单 | 🤩🤩🤩 |
155+
| [1470. 重新排列数组](https://leetcode.cn/problems/shuffle-the-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/shuffle-the-array/solution/by-ac_oier-3lck/) | 简单 | 🤩🤩🤩🤩🤩 |
155156
| [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 |
156157
| [1486. 数组异或操作](https://leetcode-cn.com/problems/xor-operation-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-operation-in-an-array/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-dggg/) | 简单 | 🤩🤩🤩 |
157158
| [1518. 换酒问题](https://leetcode-cn.com/problems/water-bottles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/water-bottles/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-sh-7yyo/) | 简单 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1470. 重新排列数组](https://leetcode.cn/problems/shuffle-the-array/solution/by-ac_oier-3lck/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个数组 `nums`,数组中有 `2n` 个元素,按 $[x_1,x_2,...,x_n,y_1,y_2,...,y_n]$ 的格式排列。
10+
11+
请你将数组按 $[x_1,y_1,x_2,y_2,...,x_n,y_n]$ 格式重新排列,返回重排后的数组。
12+
13+
示例 1:
14+
```
15+
输入:nums = [2,5,1,3,4,7], n = 3
16+
17+
输出:[2,3,5,4,1,7]
18+
19+
解释:由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ,所以答案为 [2,3,5,4,1,7]
20+
```
21+
示例 2:
22+
```
23+
输入:nums = [1,2,3,4,4,3,2,1], n = 4
24+
25+
输出:[1,4,2,3,3,2,4,1]
26+
```
27+
示例 3:
28+
```
29+
输入:nums = [1,1,2,2], n = 2
30+
31+
输出:[1,2,1,2]
32+
```
33+
34+
提示:
35+
* $1 <= n <= 500$
36+
* $nums.length = 2 \times n$
37+
* $1 <= nums[i] <= 10^3$
38+
39+
---
40+
41+
### 模拟
42+
43+
根据题意进行模拟即可。
44+
45+
Java 代码:
46+
```Java
47+
class Solution {
48+
public int[] shuffle(int[] nums, int n) {
49+
int[] ans = new int[2 * n];
50+
for (int i = 0, j = n, k = 0; k < 2 * n; k++) {
51+
ans[k] = k % 2 == 0 ? nums[i++] : nums[j++];
52+
}
53+
return ans;
54+
}
55+
}
56+
```
57+
Typescript 代码:
58+
```Typescript
59+
function shuffle(nums: number[], n: number): number[] {
60+
const ans = new Array<number>()
61+
for (let i = 0, j = n, k = 0; k < 2 * n; k++) {
62+
ans.push(k % 2 == 0 ? nums[i++] : nums[j++])
63+
}
64+
return ans
65+
};
66+
```
67+
* 时间复杂度:$O(n)$
68+
* 空间复杂度:$O(n)$
69+
70+
---
71+
72+
### 加餐
73+
74+
**加餐一道其他题目 [关于 RMQ 的若干解法](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493262&idx=1&sn=2d8e192a5767b49b9a13a6192ab3b833) 🎉🎉**
75+
76+
---
77+
78+
### 最后
79+
80+
这是我们「刷穿 LeetCode」系列文章的第 `No.1470` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
81+
82+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
83+
84+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
85+
86+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
87+

0 commit comments

Comments
 (0)