Skip to content

Commit 8d4f87d

Browse files
committed
update: 添加问题“1920.基于排列构建数组”的代码和题解(#920)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 9ff5f88 commit 8d4f87d

8 files changed

+214
-4
lines changed

.commitmsg

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-06 21:30:01
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-06 21:32:46
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<int> buildArray(vector<int>& nums) {
14+
vector<int> ans(nums.size());
15+
for (int i = 0; i < nums.size(); i++) {
16+
ans[i] = nums[nums[i]];
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-06 21:30:01
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-06 21:39:38
6+
*/
7+
package main
8+
9+
func buildArray(nums []int) []int {
10+
ans := make([]int, len(nums))
11+
for i := range nums {
12+
ans[i] = nums[nums[i]]
13+
}
14+
return ans
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-06 21:30:01
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-06 21:37:52
6+
*/
7+
class Solution {
8+
public int[] buildArray(int[] nums) {
9+
int[] ans = new int[nums.length];
10+
for (int i = 0; i < nums.length; i++) {
11+
ans[i] = nums[nums[i]];
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-06 21:30:01
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-06 21:34:14
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def buildArray(self, nums: List[int]) -> List[int]:
11+
ans = [0] * len(nums)
12+
for i in range(len(nums)):
13+
ans[i] = nums[nums[i]]
14+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@
632632
|1884.鸡蛋掉落-两枚鸡蛋|中等|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/13/LeetCode%201884.%E9%B8%A1%E8%9B%8B%E6%8E%89%E8%90%BD-%E4%B8%A4%E6%9E%9A%E9%B8%A1%E8%9B%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142906976" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/solutions/2949710/letmefly-1884ji-dan-diao-luo-liang-mei-j-saz6/" target="_blank">LeetCode题解</a>|
633633
|1901.寻找峰值II|中等|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/19/LeetCode%201901.%E5%AF%BB%E6%89%BE%E5%B3%B0%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135083347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/solutions/2572012/letmefly-1901xun-zhao-feng-zhi-iier-fen-19tmj/" target="_blank">LeetCode题解</a>|
634634
|1911.最大子序列交替和|中等|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/11/LeetCode%201911.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%88%97%E4%BA%A4%E6%9B%BF%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131652316" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/solutions/2339029/letmefly-1911zui-da-zi-xu-lie-jiao-ti-he-fyzq/" target="_blank">LeetCode题解</a>|
635+
|1920.基于排列构建数组|简单|<a href="https://leetcode.cn/problems/build-array-from-permutation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/06/LeetCode%201920.%E5%9F%BA%E4%BA%8E%E6%8E%92%E5%88%97%E6%9E%84%E5%BB%BA%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147748208" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/build-array-from-permutation/solutions/3670248/letmefly-1920ji-yu-pai-lie-gou-jian-shu-p0pif/" target="_blank">LeetCode题解</a>|
635636
|1922.统计好数字的数目|中等|<a href="https://leetcode.cn/problems/count-good-numbers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/14/LeetCode%201922.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E6%95%B0%E5%AD%97%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147200001" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-good-numbers/solutions/3650327/letmefly-1922tong-ji-hao-shu-zi-de-shu-m-ev7k/" target="_blank">LeetCode题解</a>|
636637
|1928.规定时间内到达终点的最小花费|困难|<a href="https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/03/LeetCode%201928.%E8%A7%84%E5%AE%9A%E6%97%B6%E9%97%B4%E5%86%85%E5%88%B0%E8%BE%BE%E7%BB%88%E7%82%B9%E7%9A%84%E6%9C%80%E5%B0%8F%E8%8A%B1%E8%B4%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142691241" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/solutions/2937779/letmefly-1928gui-ding-shi-jian-nei-dao-d-h1rk/" target="_blank">LeetCode题解</a>|
637638
|1944.队列中可以看到的人数|困难|<a href="https://leetcode.cn/problems/number-of-visible-people-in-a-queue/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/05/LeetCode%201944.%E9%98%9F%E5%88%97%E4%B8%AD%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E7%9A%84%E4%BA%BA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135416441" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-visible-people-in-a-queue/solutions/2592403/letmefly-1944dui-lie-zhong-ke-yi-kan-dao-e8p6/" target="_blank">LeetCode题解</a>|
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: 1920.基于排列构建数组
3+
date: 2025-05-06 21:41:37
4+
tags: [题解, LeetCode, 简单, 数组, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1920.基于排列构建数组
9+
10+
力扣题目链接:[https://leetcode.cn/problems/build-array-from-permutation/](https://leetcode.cn/problems/build-array-from-permutation/)
11+
12+
<p>给你一个 <strong>从 0 开始的排列</strong> <code>nums</code>(<strong>下标也从 0 开始</strong>)。请你构建一个 <strong>同样长度</strong> 的数组 <code>ans</code> ,其中,对于每个 <code>i</code>(<code>0 &lt;= i &lt; nums.length</code>),都满足 <code>ans[i] = nums[nums[i]]</code> 。返回构建好的数组 <code>ans</code> 。</p>
13+
14+
<p><strong>从 0 开始的排列</strong> <code>nums</code> 是一个由 <code>0</code> 到&nbsp;<code>nums.length - 1</code>(<code>0</code> 和 <code>nums.length - 1</code> 也包含在内)的不同整数组成的数组。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong>示例 1:</strong></p>
19+
20+
<pre>
21+
<strong>输入:</strong>nums = [0,2,1,5,3,4]
22+
<strong>输出:</strong>[0,1,2,4,5,3]<strong>
23+
解释:</strong>数组 ans 构建如下:
24+
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
25+
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
26+
= [0,1,2,4,5,3]</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>nums = [5,0,1,2,3,4]
32+
<strong>输出:</strong>[4,5,0,1,2,3]
33+
<strong>解释:</strong>数组 ans 构建如下:
34+
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
35+
= [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
36+
= [4,5,0,1,2,3]</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
44+
<li><code>0 &lt;= nums[i] &lt; nums.length</code></li>
45+
<li><code>nums</code> 中的元素 <strong>互不相同</strong></li>
46+
</ul>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>进阶:</strong>你能在不使用额外空间的情况下解决此问题吗(即 <code>O(1)</code> 内存)?</p>
51+
52+
53+
54+
## 解题方法:模拟
55+
56+
创建一个长度和`nums`相等的数组,从$0$到$len(nums)-1$遍历,令$ans[i]$的值为$nums[nums[i]]$即可。
57+
58+
+ 时间复杂度$O(len(nums))$
59+
+ 空间复杂度$O(1)$,力扣算法返回值不计入算法空间复杂度
60+
61+
### AC代码
62+
63+
#### C++
64+
65+
```cpp
66+
/*
67+
* @Author: LetMeFly
68+
* @Date: 2025-05-06 21:30:01
69+
* @LastEditors: LetMeFly.xyz
70+
* @LastEditTime: 2025-05-06 21:32:46
71+
*/
72+
class Solution {
73+
public:
74+
vector<int> buildArray(vector<int>& nums) {
75+
vector<int> ans(nums.size());
76+
for (int i = 0; i < nums.size(); i++) {
77+
ans[i] = nums[nums[i]];
78+
}
79+
return ans;
80+
}
81+
};
82+
```
83+
84+
#### Python
85+
86+
```python
87+
'''
88+
Author: LetMeFly
89+
Date: 2025-05-06 21:30:01
90+
LastEditors: LetMeFly.xyz
91+
LastEditTime: 2025-05-06 21:34:14
92+
'''
93+
from typing import List
94+
95+
class Solution:
96+
def buildArray(self, nums: List[int]) -> List[int]:
97+
ans = [0] * len(nums)
98+
for i in range(len(nums)):
99+
ans[i] = nums[nums[i]]
100+
return ans
101+
```
102+
103+
#### Java
104+
105+
```java
106+
/*
107+
* @Author: LetMeFly
108+
* @Date: 2025-05-06 21:30:01
109+
* @LastEditors: LetMeFly.xyz
110+
* @LastEditTime: 2025-05-06 21:37:52
111+
*/
112+
class Solution {
113+
public int[] buildArray(int[] nums) {
114+
int[] ans = new int[nums.length];
115+
for (int i = 0; i < nums.length; i++) {
116+
ans[i] = nums[nums[i]];
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
#### Go
124+
125+
```go
126+
/*
127+
* @Author: LetMeFly
128+
* @Date: 2025-05-06 21:30:01
129+
* @LastEditors: LetMeFly.xyz
130+
* @LastEditTime: 2025-05-06 21:39:38
131+
*/
132+
package main
133+
134+
func buildArray(nums []int) []int {
135+
ans := make([]int, len(nums))
136+
for i := range nums {
137+
ans[i] = nums[nums[i]]
138+
}
139+
return ans
140+
}
141+
```
142+
143+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147748208)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/05/06/LeetCode%201920.%E5%9F%BA%E4%BA%8E%E6%8E%92%E5%88%97%E6%9E%84%E5%BB%BA%E6%95%B0%E7%BB%84/)哦~
144+
>
145+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ xx型
374374
|ベタベタ|黏糊糊的|
375375
|ボロボロ|破破烂烂的|
376376
|親切(しんせつ)|亲切的|
377+
|残念(ざんねん)|遗憾的|
377378
|||
378379
|きっぷ||
379380
|チケット||
@@ -515,6 +516,8 @@ xx型
515516
|ワイシャツ|白衬衫|
516517
|スーツ|西装|
517518
|スリッパ|拖鞋|
519+
|アプリ|应用|
520+
|スマホ|智能手机|
518521
|||
519522
|プール|游泳池|
520523
|ふる|浴室|
@@ -1333,7 +1336,7 @@ xx型
13331336
|必ず悪いレビューをたくさん書きます。<br/>我一定会写很多差评!|
13341337
|列で二時間待つました。<br/>我排了两个小时队。|
13351338
|先週のデートはどうでしたか?<br/>上周的约会怎么样?|
1336-
|<br/>|
1339+
|残念なデート。<br/>令人失望的约会。|
13371340
|<br/>|
13381341
|<br/>|
13391342
|<br/>|

0 commit comments

Comments
 (0)