[LeetCode] Container With Most Water 简要分析
2015-12-04 16:42
483 查看
前言
这题非要说贪心的话也算是吧,不过最主要的特征还是双指针。LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的。这道题倒是“短板效应”的不错体现了。
题目
Givennnon-negativeintegersa1,a2,…,an,whereeachrepresentsapointatcoordinate(i,ai).nverticallinesaredrawnsuchthatthetwoendpointsoflineiisat(i,ai)and(i,0).Findtwolines,whichtogetherwithx-axisformsacontainer,suchthatthecontainercontainsthemostwater.
Note:Youmaynotslantthecontainer.
思路
图片看不清楚的话可右键复制链接转到图床再看。
代码
classSolution{ public: intmaxArea(vector<int>&height){ intfirst=0; intend=height.size()-1; intresult=INT_MIN; while(first<end) { intwidth=end-first; intboard=height[end]<height[first]?height[end]:height[first]; intarea=board*width; result=result>area?result:area; if(height[first]<=height[end]) first++; else end--; } returnresult; } };
其他
JAVA果然过得比CPP快,Python和C#依旧垫底。
相关文章推荐
- 【LeetCode】219 Contains Duplicate II
- AC 与 GLPI的MAC地址自动同步脚本 expect mail ver4.0【最新】
- for update 和for update nowait
- SCP报错:Host key verification failed
- 杭电-2674N!Again(大数阶乘)
- poj3757 Training little cats
- 理解Explain命令输出中的filesort
- Exception in thread "main" org.hibernate.SessionException: Session is closed!
- Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
- AEAI DP开发平台精要
- HDOJ 2674-N!Again【数论】
- Contains Duplicate
- UVa 10651 Pebble Solitaire(状压DP)
- fail2ban安装配置
- sleep()和wait()有什么区别
- socket failed:EACCES(Permission denied)
- typearray和obtainStyledAttribute的作用
- iar for stm8 报错Error[Li005]: no definition for "assert_failed"解决方案
- iar for stm8 报错Error[Li005]: no definition for "assert_failed"解决方案
- linq中的contains条件