2014年3月11日

LeetCode: Trapping Rain Water

摘要: Givennnon-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.这个题的思路应该是,根据当前bar的左右两侧的最高的bar的高度,可以计算出当前bar可以储存的水。所以用三个循环。第一个从左到右扫描,保存下每个bar左侧最高bar的高度。第二个从右到左,保存下每个bar右侧最高bar的高度。最后通过前两个计算出每个bar可以存水多少,加和。 1 public int 阅读全文

posted @ 2014-03-11 11:34 longhorn 阅读(184) 评论(0) 推荐(0)

LeetCode: Distinct Subsequences

摘要: Given a stringSand a stringT, count the number of distinct subsequences ofTinS.最开始,一感觉,这就是一个DFS。所以也就按dfs写了。但是超时。想到了dp,但是觉得无法解决。因为当时想到的是1维动态规划,感觉这个问题,无法用子问题得到答案。看到网上的解法,最后还是用的dp,不过是二维dp。map[i][j]表示的是S中的前i个字符,有多少种方式可以表示T中的前j个字符。if S[i] == T[j], map[i][j] = map[i-1][j] + map[i-1][j-1];if S[i] != T[j], 阅读全文

posted @ 2014-03-11 03:27 longhorn 阅读(183) 评论(0) 推荐(0)

导航