LeetCode 11 Container With Most Water
2015-06-12 13:35
537 查看
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
思路一:暴力解法,算出每两个短板之间的装水量,结果超时。
View Code
Note: You may not slant the container.
思路一:暴力解法,算出每两个短板之间的装水量,结果超时。
public class Solution { public int maxArea(int[] height) { int i=0; int j=height.length-1; int ans=0; while(i<j){ int temp=(height[i]>height[j]?height[j]:height[i])*(j-i); if(temp>ans){ ans=temp; } if(height[i]<height[j]){ i++; }else{ j--; } } return ans; } }
View Code
相关文章推荐
- $GetDomesticAirlinesTimeResult_type0@2bb5340c类错误
- HDOJ 1021 Fibonacci Again
- 十大经典数据挖掘算法(9) 朴素贝叶斯分类器 Naive Bayes
- leetcode之Contains Duplicate
- 大麦回忆录-maizuo开发
- Handler sendMessage 与 obtainMessage (sendToTarget)比较
- Leetcode220-Contains Duplicate III
- Compiler Error Message: The compiler failed with error code 128. 的问题解决
- http://blog.csdn.net/name_Uncle_Wang/article/details/46438641?ref=myread
- 出现set_locale() failed (5): Input/output error的解决方法
- scanf问题
- Climbing Stairs爬楼梯——动态规划
- 9 Ways to Instantly Strengthen Your Brain
- [POJ 1273] Drainage Ditches & 最大流Dinic模板
- SceneRenderer_Main中的方法
- 记使用WaitGroup时的一个错误
- Training:组合博弈入门
- MediaStore.Images.Thumbnails
- AI 笔记(一)
- hdu 1848 Fibonacci again and again