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 1299
2
+
3
+ tags : Array
4
+
5
+ 方法1 :
6
+ 这个是我自己的方法 ,纯粹是intuitive的 。就是n遍循环
7
+
8
+ 方法2 :
9
+ 没有从正向循环 ,而是从反向循环 ,巧妙的存储了max
10
+ 利用Math .max省略了少存储一次arr [i ]也很巧妙
11
+
12
+
13
+ ```
14
+
15
+ /*
16
+ 方法1
17
+ */
18
+
19
+ class Solution {
20
+ public int [] replaceElements (int [] arr ) {
21
+ int [] res = new int [arr .length ];
22
+ for (int i = 0 ; i < arr .length - 1 ; i ++) {
23
+ int max = Integer .MIN_VALUE ;
24
+ for (int j = i + 1 ; j < arr .length ; j ++) {
25
+ if (arr [j ] > max ) {
26
+ max = arr [j ];
27
+ }
28
+ }
29
+ res [i ] = max ;
30
+ }
31
+ res [arr .length -1 ] = -1 ;
32
+ return res ;
33
+ }
34
+ }
35
+
36
+ /*
37
+ 方法2
38
+ */
39
+
40
+ class Solution {
41
+ public int [] replaceElements (int [] arr ) {
42
+ int max = -1 ;
43
+ for (int i = arr .length -1 ; i >= 0 ; i --) {
44
+ max = Math .max (arr [i ], arr [i ] = max );
45
+ }
46
+ return arr ;
47
+ }
48
+ }
49
+
50
+
51
+
52
+
53
+
You can’t perform that action at this time.
0 commit comments