Skip to content

Commit 75289ac

Browse files
author
Ysc
committed
5/25
1 parent 12aea8a commit 75289ac

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 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+

0 commit comments

Comments
 (0)