File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments