Skip to content

Commit c676e46

Browse files
committed
update
1 parent 1912623 commit c676e46

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "正则表达式匹配2"
5+
tags : [LeetCode,面试,正则匹配,贪心,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
> Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
12+
>
13+
>For example,
14+
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
15+
16+
![img/leetcode_TrappingRainWater.png](img/leetcode_TrappingRainWater.png)
17+
18+
解法:被虐了,一种比较好理解的方法是,用`S[i]`标记第`i`列能放多少水。他取决与`i`之前的最大高度和`i`之后的最大高度,则至少需要开辟一个数组来存一侧的最大高度。然后遍历另一侧时顺便求解。
19+
20+
另外一种方法是Discuss中的,两个游标,一个从前遍历,一个从后遍历,记录总的水量、总的水量中黑块的个数、当前的Level。算水量时把黑块部分也计算在内,以后再减。
21+
22+
空间复杂度为常量,代码如下:
23+
24+
//{java}
25+
public class Solution {
26+
public int trap(int[] A) {
27+
if(A.length==0)
28+
return 0;
29+
int block=0,all=0,level=0,s=0,e=A.length-1;
30+
while(s<=e)
31+
{
32+
int t = min(A[s],A[e]);
33+
if(t>level)
34+
{
35+
all+=(t-level)*(e-s+1);
36+
level=t;
37+
}
38+
if(A[s]<A[e])
39+
block+=A[s++];
40+
else
41+
block+=A[e--];
42+
}
43+
return all-block;
44+
}
45+
46+
public int min(int a,int b)
47+
{
48+
return a>b?b:a;
49+
}
50+
}

‎_posts/2014-07-17-LeetCode之Wildcard-Matching.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: post
33
category : LeetCode
44
tagline: "正则表达式匹配2"
5-
tags : [LeetCode,面试,正则匹配,贪心,递归]
5+
tags : [LeetCode,面试,正则匹配,贪心,递归]
66
---
77
{% include JB/setup %}
88

‎img/leetcode_TrappingRainWater.png‎

7.66 KB
Loading

0 commit comments

Comments
 (0)