刷题42. Trapping Rain Water
一、题目说明
题目是42. Trapping Rain Water,翻译起来就是“接雨水”。给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水。题目难度是Hard
二、我的解法
这个题目是找“坑”,然后计算里面可以存的“雨”。总共提交了5次,前面几次都是边界错误。
代码如下:
#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
int trap(vector<int>& height){
if(height.size()<1) return 0;
int len = height.size();
int sum = 0,area=0,h;
bool lflag = false,rflag = false;
int left=0,leftStart,right,rightEnd=len-1,mid;
while(left<rightEnd){
//从左边开始找第1个高度
leftStart = left;
while(leftStart<len-1 && height[leftStart]<=height[leftStart+1]){
leftStart++;
}
left = leftStart;
//从右边开始找第1个高度
right = rightEnd;
while(right>left && height[right]<=height[right-1]){
right--;
}
rightEnd = right;
if(height[rightEnd]<=height[left]){
right = rightEnd;
//降
while(right>left && (height[right]<=height[rightEnd])){
right--;
}
//升
while(right>left && (height[right]<height[right-1])){
right--;
}
h = height[right]<height[rightEnd] ? height[right]: height[rightEnd];
area = 0;
for(int t=right+1;t<rightEnd;t++){
if(h>height[t]){
area = area + (h-height[t]);
}
}
sum += area;
rightEnd = right;
}else{
leftStart = left;
//降
while(left<rightEnd && (height[left]<=height[leftStart])){
left++;
}
//升
while(left<rightEnd && (height[left]<height[left-1])){
left++;
}
h = height[left]<height[leftStart] ? height[left]: height[leftStart];
area = 0;
for(int t=leftStart+1;t<left;t++){
if(h>height[t]){
area = area + (h-height[t]);
}
}
sum += area;
leftStart = left;
}
}
return sum;
}
};
int main(){
Solution s;
vector<int> r;
r = {0,1,0,2,1,0,1,3,2,1,2,1};
cout<<s.trap(r)<<":"<<(6==s.trap(r))<<"\n";
r = {5,4,1,2};
cout<<s.trap(r)<<":"<<(1==s.trap(r))<<"\n";
r = {5,2,1,2,1,5};
cout<<s.trap(r)<<":"<<(14==s.trap(r))<<"\n";
r = {5,5,1,7,1,1,5,2,7,6};
cout<<s.trap(r)<<":"<<(23==s.trap(r))<<"\n";
r = {6,4,2,0,3,2,0,3,1,4,5,3,2,7,5,3,0,1,2,1,3,4,6,8,1,3};
cout<<s.trap(r)<<":"<<(83==s.trap(r))<<"\n";
return 0;
}
性能如下:
Runtime: 8 ms, faster than 61.40% of C++ online submissions for Trapping Rain Water.
Memory Usage: 9.1 MB, less than 91.14% of C++ online submissions for Trapping Rain Water.
三、优化措施
代码虽然正确,但看起来很难过!多番寻找,相对优雅的代码如下:
class Solution{
public:
//left、right
int trap(vector<int>& height) {
int n = height.size();
int lhigh = 0, rhigh = n-1;
int diff = 0;
// scan from left to right
for(int i = lhigh; i<n; i++)
{
if (height[i] < height[lhigh]) continue;
for (int j = lhigh+1; j<i; j++) diff += height[lhigh]-height[j];
lhigh = i;
}
// scan from right to left
for (int i = rhigh; i>=lhigh; i--)
{
if (height[i] < height[rhigh]) continue;
for (int j = i+1; j<rhigh; j++) diff += height[rhigh]-height[j];
rhigh = i;
}
return diff;
}
};
性能虽然差点,但可读性好多了。
Runtime: 12 ms, faster than 17.25% of C++ online submissions for Trapping Rain Water.
Memory Usage: 9 MB, less than 94.94% of C++ online submissions for Trapping Rain Water.
刷题42. Trapping Rain Water的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- REST接口
全名是Representational State Transfer REST是设计风格而不是标准 建议将JSON格式作为标准响应格式 -------------------------------- ...
- BeginInvoke之前检测句柄
只要在BeginInvoke方法的调用语句前再加一句:IntPtr i = this.Handle;就OK了,这比死循环配合this.IsHandleCreated的判断方法更简洁,因为this.Ha ...
- spring第9天(事务)
依赖:spring-context,spring-jdbc(这个本身有依赖spring-tx,关于事务的),druid,mysql-connector-java,aspectjweaver五个 由于我 ...
- 编程入门-Eclipse项目导出和导入
编程入门-Eclipse项目导出和导入 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.导出项目 1>.如下图所示,在项目目录上右击鼠标,依次点击"Export& ...
- springboot + shiro+登录 微信登录 次数验证资料
分享一个朋友的人工智能教程.零基础!通俗易懂!风趣幽默!大家可以看看是否对自己有帮助,点击查看教程. 1.https://blog.csdn.net/xtiawxf/article/details/5 ...
- 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现
文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...
- Vue.js(18)之 axios简单封装
基于vue-cli2.x封装axios src目录 axios.js import axios from 'axios' import { Indicator, Toast } from 'mint- ...
- maven学习(三)-使用maven来创建项目
转自https://www.cnblogs.com/xdp-gacl/p/4240930.html maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项 ...
- POJ - 3659 Cell Phone Network(树形dp---树的最小点支配集)
题意:有N个点,N-1条边,任意两点可达,由此形成了一棵树.选取一个点a,它可覆盖自己以及与自己相邻的点,选取尽量少的点a,使得树中所有点都被覆盖,即求树的最小点支配集. 分析: 1.对于每一个点cu ...
- JZOJ-2019-11-8 A组
T1 给定\(n\)个点的坐标(\(0 \leq xi,yi \leq 10000\))求选出任意三个点能组成的三角形的总面积. Input 第一行\(n\)表示点数.接下来每行两个数\(x_i\), ...