Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f76309e

Browse files
feat: add solutions to lc problem: No.1441 (doocs#4268)
No.1441.Build an Array With Stack Operations
1 parent f563c72 commit f76309e

File tree

9 files changed

+182
-157
lines changed

9 files changed

+182
-157
lines changed

‎solution/1400-1499/1441.Build an Array With Stack Operations/README.md‎

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<pre>
4242
<strong>输入:</strong>target = [1,3], n = 3
4343
<strong>输出:</strong>["Push","Push","Pop","Push"]
44-
<strong>解释:
44+
<strong>解释:
4545
</strong>读取 1 并自动推入数组 -&gt; [1]
4646
读取 2 并自动推入数组,然后删除它 -&gt; [1]
4747
读取 3 并自动推入数组 -&gt; [1,3]
@@ -81,13 +81,17 @@ tags:
8181

8282
### 方法一:模拟
8383

84-
我们定义 $cur$ 表示当前已经从 `list` 中读取到的数字,初始时 $cur = 0,ドル用一个数组 $ans$ 存储答案。
84+
我们定义一个变量 $\textit{cur}$ 表示当前待读取的数字,初始时 $\textit{cur} = 1,ドル用一个数组 $\textit{ans}$ 存储答案。
8585

86-
遍历数组 `target`,对于每个数字 $v,ドル如果当前要从 `list` 读取的数字小于 $v,ドル那么我们应该执行 `Push``Pop` 操作,直到读取的数字等于 $v,ドル然后执行 `Push` 操作,这样就可以得到数字 $v$。
86+
接下来,我们遍历数组 $\textit{target}$ 中的每个数字 $x$:
8787

88-
遍历结束后,也就构建出了数组 `target`,返回 `ans` 即可。
88+
- 如果 $\textit{cur} < x,ドル我们将 $\textit{Push}$ 和 $\textit{Pop}$ 依次加入答案,直到 $\textit{cur} = x$;
89+
- 然后我们将 $\textit{Push}$ 加入答案,表示读取数字 $x$;
90+
- 接着,我们将 $\textit{cur}$ 加一,继续处理下一个数字。
8991

90-
时间复杂度 $O(n),ドル其中 $n$ 为数组 `target` 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
92+
遍历结束后,返回答案数组即可。
93+
94+
时间复杂度 $O(n),ドル其中 $n$ 是数组 $\textit{target}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
9195

9296
<!-- tabs:start -->
9397

@@ -96,13 +100,14 @@ tags:
96100
```python
97101
class Solution:
98102
def buildArray(self, target: List[int], n: int) -> List[str]:
99-
cur, ans =0, []
100-
for v in target:
101-
cur +=1
102-
while cur < v:
103-
ans.extend(['Push', 'Pop'])
103+
ans = []
104+
cur =1
105+
for x in target:
106+
while cur < x:
107+
ans.extend(["Push", "Pop"])
104108
cur += 1
105-
ans.append('Push')
109+
ans.append("Push")
110+
cur += 1
106111
return ans
107112
```
108113

@@ -111,14 +116,15 @@ class Solution:
111116
```java
112117
class Solution {
113118
public List<String> buildArray(int[] target, int n) {
114-
int cur = 0;
115119
List<String> ans = new ArrayList<>();
116-
for (int v : target) {
117-
while (++cur < v) {
118-
ans.add("Push");
119-
ans.add("Pop");
120+
int cur = 1;
121+
for (int x : target) {
122+
while (cur < x) {
123+
ans.addAll(List.of("Push", "Pop"));
124+
++cur;
120125
}
121126
ans.add("Push");
127+
++cur;
122128
}
123129
return ans;
124130
}
@@ -131,14 +137,16 @@ class Solution {
131137
class Solution {
132138
public:
133139
vector<string> buildArray(vector<int>& target, int n) {
134-
int cur = 0;
135140
vector<string> ans;
136-
for (int& v : target) {
137-
while (++cur < v) {
138-
ans.emplace_back("Push");
139-
ans.emplace_back("Pop");
141+
int cur = 1;
142+
for (int x : target) {
143+
while (cur < x) {
144+
ans.push_back("Push");
145+
ans.push_back("Pop");
146+
++cur;
140147
}
141-
ans.emplace_back("Push");
148+
ans.push_back("Push");
149+
++cur;
142150
}
143151
return ans;
144152
}
@@ -148,32 +156,33 @@ public:
148156
#### Go
149157
150158
```go
151-
func buildArray(target []int, n int) []string {
152-
cur := 0
153-
ans := []string{}
154-
for _, v := range target {
155-
for cur = cur + 1; cur < v; cur++ {
159+
func buildArray(target []int, n int) (ans []string) {
160+
cur := 1
161+
for _, x := range target {
162+
for ; cur < x; cur++ {
156163
ans = append(ans, "Push", "Pop")
157164
}
158165
ans = append(ans, "Push")
166+
cur++
159167
}
160-
return ans
168+
return
161169
}
162170
```
163171

164172
#### TypeScript
165173

166174
```ts
167175
function buildArray(target: number[], n: number): string[] {
168-
const res = [];
169-
let cur=0;
170-
for (const num of target) {
171-
while (++cur < num) {
172-
res.push('Push', 'Pop');
176+
const ans:string[] = [];
177+
let cur:number=1;
178+
for (const x of target) {
179+
for (; cur < x; ++cur) {
180+
ans.push('Push', 'Pop');
173181
}
174-
res.push('Push');
182+
ans.push('Push');
183+
++cur;
175184
}
176-
return res;
185+
return ans;
177186
}
178187
```
179188

@@ -182,18 +191,18 @@ function buildArray(target: number[], n: number): string[] {
182191
```rust
183192
impl Solution {
184193
pub fn build_array(target: Vec<i32>, n: i32) -> Vec<String> {
185-
let mut res = Vec::new();
194+
let mut ans = Vec::new();
186195
let mut cur = 1;
187-
for &num in target.iter() {
188-
while cur < num {
189-
res.push("Push");
190-
res.push("Pop");
196+
for &x in &target {
197+
while cur < x {
198+
ans.push("Push".to_string());
199+
ans.push("Pop".to_string());
191200
cur += 1;
192201
}
193-
res.push("Push");
202+
ans.push("Push".to_string());
194203
cur += 1;
195204
}
196-
res.into_iter().map(String::from).collect()
205+
ans
197206
}
198207
}
199208
```
@@ -205,21 +214,19 @@ impl Solution {
205214
* Note: The returned array must be malloced, assume caller calls free().
206215
*/
207216
char** buildArray(int* target, int targetSize, int n, int* returnSize) {
208-
char** res = (char**) malloc(sizeof(char*) * n * 2);
217+
char** ans = (char**) malloc(sizeof(char*) * (2 * n));
218+
*returnSize = 0;
209219
int cur = 1;
210-
int i = 0;
211-
for (int j = 0; j < targetSize; j++) {
212-
while (++cur < target[j]) {
213-
res[i] = (char*) malloc(sizeof(char) * 8);
214-
strcpy(res[i++], "Push");
215-
res[i] = (char*) malloc(sizeof(char) * 8);
216-
strcpy(res[i++], "Pop");
220+
for (int i = 0; i < targetSize; i++) {
221+
while (cur < target[i]) {
222+
ans[(*returnSize)++] = "Push";
223+
ans[(*returnSize)++] = "Pop";
224+
cur++;
217225
}
218-
res[i] = (char*) malloc(sizeof(char) * 8);
219-
strcpy(res[i++], "Push");
226+
ans[(*returnSize)++] = "Push";
227+
cur++;
220228
}
221-
*returnSize = i;
222-
return res;
229+
return ans;
223230
}
224231
```
225232

‎solution/1400-1499/1441.Build an Array With Stack Operations/README_EN.md‎

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,19 @@ The answers that read integer 3 from the stream are not accepted.
9393

9494
<!-- solution:start -->
9595

96-
### Solution 1
96+
### Solution 1: Simulation
97+
98+
We define a variable $\textit{cur}$ to represent the current number to be read, initially set to $\textit{cur} = 1,ドル and use an array $\textit{ans}$ to store the answer.
99+
100+
Next, we iterate through each number $x$ in the array $\textit{target}$:
101+
102+
- If $\textit{cur} < x,ドル we add $\textit{Push}$ and $\textit{Pop}$ to the answer alternately until $\textit{cur} = x$;
103+
- Then we add $\textit{Push}$ to the answer, representing reading the number $x$;
104+
- After that, we increment $\textit{cur}$ and continue to process the next number.
105+
106+
After the iteration, we return the answer array.
107+
108+
The time complexity is $O(n),ドル where $n$ is the length of the array $\textit{target}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
97109

98110
<!-- tabs:start -->
99111

@@ -102,13 +114,14 @@ The answers that read integer 3 from the stream are not accepted.
102114
```python
103115
class Solution:
104116
def buildArray(self, target: List[int], n: int) -> List[str]:
105-
cur, ans =0, []
106-
for v in target:
107-
cur +=1
108-
while cur < v:
109-
ans.extend(['Push', 'Pop'])
117+
ans = []
118+
cur =1
119+
for x in target:
120+
while cur < x:
121+
ans.extend(["Push", "Pop"])
110122
cur += 1
111-
ans.append('Push')
123+
ans.append("Push")
124+
cur += 1
112125
return ans
113126
```
114127

@@ -117,14 +130,15 @@ class Solution:
117130
```java
118131
class Solution {
119132
public List<String> buildArray(int[] target, int n) {
120-
int cur = 0;
121133
List<String> ans = new ArrayList<>();
122-
for (int v : target) {
123-
while (++cur < v) {
124-
ans.add("Push");
125-
ans.add("Pop");
134+
int cur = 1;
135+
for (int x : target) {
136+
while (cur < x) {
137+
ans.addAll(List.of("Push", "Pop"));
138+
++cur;
126139
}
127140
ans.add("Push");
141+
++cur;
128142
}
129143
return ans;
130144
}
@@ -137,14 +151,16 @@ class Solution {
137151
class Solution {
138152
public:
139153
vector<string> buildArray(vector<int>& target, int n) {
140-
int cur = 0;
141154
vector<string> ans;
142-
for (int& v : target) {
143-
while (++cur < v) {
144-
ans.emplace_back("Push");
145-
ans.emplace_back("Pop");
155+
int cur = 1;
156+
for (int x : target) {
157+
while (cur < x) {
158+
ans.push_back("Push");
159+
ans.push_back("Pop");
160+
++cur;
146161
}
147-
ans.emplace_back("Push");
162+
ans.push_back("Push");
163+
++cur;
148164
}
149165
return ans;
150166
}
@@ -154,32 +170,33 @@ public:
154170
#### Go
155171
156172
```go
157-
func buildArray(target []int, n int) []string {
158-
cur := 0
159-
ans := []string{}
160-
for _, v := range target {
161-
for cur = cur + 1; cur < v; cur++ {
173+
func buildArray(target []int, n int) (ans []string) {
174+
cur := 1
175+
for _, x := range target {
176+
for ; cur < x; cur++ {
162177
ans = append(ans, "Push", "Pop")
163178
}
164179
ans = append(ans, "Push")
180+
cur++
165181
}
166-
return ans
182+
return
167183
}
168184
```
169185

170186
#### TypeScript
171187

172188
```ts
173189
function buildArray(target: number[], n: number): string[] {
174-
const res = [];
175-
let cur=0;
176-
for (const num of target) {
177-
while (++cur < num) {
178-
res.push('Push', 'Pop');
190+
const ans:string[] = [];
191+
let cur:number=1;
192+
for (const x of target) {
193+
for (; cur < x; ++cur) {
194+
ans.push('Push', 'Pop');
179195
}
180-
res.push('Push');
196+
ans.push('Push');
197+
++cur;
181198
}
182-
return res;
199+
return ans;
183200
}
184201
```
185202

@@ -188,18 +205,18 @@ function buildArray(target: number[], n: number): string[] {
188205
```rust
189206
impl Solution {
190207
pub fn build_array(target: Vec<i32>, n: i32) -> Vec<String> {
191-
let mut res = Vec::new();
208+
let mut ans = Vec::new();
192209
let mut cur = 1;
193-
for &num in target.iter() {
194-
while cur < num {
195-
res.push("Push");
196-
res.push("Pop");
210+
for &x in &target {
211+
while cur < x {
212+
ans.push("Push".to_string());
213+
ans.push("Pop".to_string());
197214
cur += 1;
198215
}
199-
res.push("Push");
216+
ans.push("Push".to_string());
200217
cur += 1;
201218
}
202-
res.into_iter().map(String::from).collect()
219+
ans
203220
}
204221
}
205222
```
@@ -211,21 +228,19 @@ impl Solution {
211228
* Note: The returned array must be malloced, assume caller calls free().
212229
*/
213230
char** buildArray(int* target, int targetSize, int n, int* returnSize) {
214-
char** res = (char**) malloc(sizeof(char*) * n * 2);
231+
char** ans = (char**) malloc(sizeof(char*) * (2 * n));
232+
*returnSize = 0;
215233
int cur = 1;
216-
int i = 0;
217-
for (int j = 0; j < targetSize; j++) {
218-
while (++cur < target[j]) {
219-
res[i] = (char*) malloc(sizeof(char) * 8);
220-
strcpy(res[i++], "Push");
221-
res[i] = (char*) malloc(sizeof(char) * 8);
222-
strcpy(res[i++], "Pop");
234+
for (int i = 0; i < targetSize; i++) {
235+
while (cur < target[i]) {
236+
ans[(*returnSize)++] = "Push";
237+
ans[(*returnSize)++] = "Pop";
238+
cur++;
223239
}
224-
res[i] = (char*) malloc(sizeof(char) * 8);
225-
strcpy(res[i++], "Push");
240+
ans[(*returnSize)++] = "Push";
241+
cur++;
226242
}
227-
*returnSize = i;
228-
return res;
243+
return ans;
229244
}
230245
```
231246

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /