leetcode 日经贴,Cpp code -Trapping Rain Water
来源:互联网 发布:尚硅谷周阳视频linux 编辑:程序博客网 时间:2024/05/16 12:13
Trapping Rain Water
class Solution {public: int trap(vector<int>& height) { int n = height.size(); stack<pair<int, int> >st; int maxh = 0, omit = 0; for (int i = 0; i < n; ++i) { while (!st.empty() && st.top().second < maxh && st.top().second < height[i]) { omit += st.top().second; st.pop(); } st.push(make_pair(i, height[i])); maxh = max(maxh, height[i]); } int water = 0; if (!st.empty()) { pair<int, int> last = st.top(); st.pop(); while (!st.empty()) { water += min(st.top().second, last.second) * (last.first - st.top().first - 1); last = st.top(); st.pop(); } } return water - omit; }};
class Solution {public: int trap(vector<int>& height) { int n = height.size(); stack<pair<int, int> >st; int maxh = 0, omit = 0; for (int i = 0; i < n; ++i) { while (!st.empty()) { int h = st.top().second; if(h < maxh && h < height[i]) { omit += h; st.pop(); } else { break; } } st.push(make_pair(i, height[i])); maxh = max(maxh, height[i]); } int water = 0; if (!st.empty()) { pair<int, int> last = st.top(); st.pop(); while (!st.empty()) { pair<int, int> cur = st.top(); water += min(cur.second, last.second) * (last.first - cur.first - 1); last = cur; st.pop(); } } return water - omit; }};
0 0
- leetcode 日经贴,Cpp code -Trapping Rain Water
- Leetcode 42. Trapping Rain Water (Hard) (cpp)
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [LeetCode]Trapping Rain Water
- LeetCode-Trapping Rain Water
- [leetcode] Trapping Rain Water
- [LeetCode] Trapping rain water
- Leetcode: Trapping Rain Water
- Leetcode:Trapping Rain Water
- [LeetCode] Trapping Rain Water
- <Leetcode>Trapping Rain Water
- Leetcode Trapping Rain Water
- leetcode Trapping Rain Water
- leetcode Trapping Rain Water
- Android开发系列 QQ登陆界面——Android控件使用实例 - Carlos.V
- 七,java类装载器
- svn Some of selected resources were not added to version control SVN错误[locked; try performing]的解决办法
- JavaScript返回前一页的两种方法 --转
- spring mvc 发送html格式velocity邮件模板(包括定时发送与按钮触发发送)
- leetcode 日经贴,Cpp code -Trapping Rain Water
- 门户网站优化经验
- Tomcat缓存设置
- linux 下tar gzip压缩命令
- 将模板类声明和实现分开
- 黑马程序员——Java基础——网络编程
- ORACLE 最大连接数的问题
- iOS 创建用xib制作的ViewController,不可以使用new方法
- 获取下一周日期