Skip to content

Commit e1db6d6

Browse files
committed
5/29
1 parent b64e13b commit e1db6d6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Easy Leetcode 1441
2+
tags: Array
3+
4+
方法1:
5+
题目不难自己的思路又是纯直接式的以后需要多思考一下如何优化算法
6+
方法2
7+
思路不错更规整一些
8+
9+
10+
```
11+
12+
/*
13+
方法1
14+
*/
15+
//import java.util.stream.IntStream;
16+
class Solution {
17+
public List<String> buildArray(int[] target, int n) {
18+
List<String> res = new LinkedList<>();
19+
for (int i = 1; i <=n; i++) {
20+
int finalI = i;
21+
boolean contains = IntStream.of(target).anyMatch(x -> x == finalI);
22+
if(contains) {
23+
24+
res.add("Push");
25+
}else{
26+
if(i > target[target.length-1]){
27+
break;
28+
}
29+
res.add("Push");
30+
res.add("Pop");
31+
}
32+
}
33+
return res;
34+
}
35+
}
36+
37+
38+
/*
39+
方法2
40+
*/
41+
public List<String> buildArray(int[] target, int n) {
42+
List<String> result = new ArrayList<>();
43+
int j=0;
44+
for (int i=1;i<=n && j<target.length;i++) {
45+
result.add("Push");
46+
if(target[j]==i) {
47+
j++;
48+
} else {
49+
result.add("Pop");
50+
}
51+
}
52+
return result;
53+
}

0 commit comments

Comments
 (0)