-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.java
More file actions
28 lines (26 loc) · 860 Bytes
/
Copy pathSolution2.java
File metadata and controls
28 lines (26 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package stack;
import java.util.Stack;
/**
* 接雨水
*/
public class Solution2 {
public int trap(int[] height) {
int result = 0;
Stack<Integer> stack = new Stack();//创建一个单调栈,是小顶单调栈
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[stack.peek()] < height[i]){
Integer top = stack.pop();
if(stack.isEmpty()){
//栈空 表示没有墙了
break;
}
//算出当前墙面与前面的墙面相隔数
int destination = i - stack.peek() - 1;
int min = Math.min(height[stack.peek()], height[i]);
result += destination*(min - height[top]);
}
stack.push(i);
}
return result;
}
}