LeetCode Container With Most Water
2014-12-04 20:10
375 查看
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.
解题思路:不说了
Note: You may not slant the container.
class Solution { public: int maxArea(vector<int> &height) { int j=height.size()-1,i=0,mx=0; while(i<j) { mx=max(mx,((j-i)*(min(height[i],height[j])))); if(height[i]<height[j]) i++; else if(height[i]>=height[j]) j--; } return mx; } }; 人家的代码,哎
class Solution { public: int min(int a,int b) { return a>b?b:a; } int maxArea(vector<int> &height) { vector<int> first,last; int max = height[0]; first.push_back(max); first.push_back(0); for(int i=0;i<height.size();i++) { if(height[i] > max) { max = height[i]; first.push_back(max); first.push_back(i); } else continue; } max = height[height.size()-1]; last.push_back(max); last.push_back(height.size()-1); for(int i=height.size()-1;i>=0;i--) { if(height[i] > max) { max = height[i]; last.push_back(max); last.push_back(i); } else continue; } max = 0; for(int i=0;i<first.size();i+=2) { for(int j=0;j<last.size();j+=2) { if(min(first[i],last[j]) * (last[j+1] - first[i+1]) > max) max = min(first[i],last[j]) * (last[j+1] - first[i+1]); } } return max; } }; 我的代码,哎
解题思路:不说了
相关文章推荐
- leetcode_Container With Most Water
- 【LEETCODE】11-Container With Most Water [Python]
- [leetcode] Container With Most Water
- leetcode:Container With Most Water 【Java】
- leetcode 【 Container With Most Water 】python 实现
- [leetcode][two pointers] Container With Most Water
- 【LeetCode】011 Container With Most Water
- [LeetCode]Container With Most Water
- [Leetcode] Container With Most Water
- Container With Most Water -- LeetCode
- leetcode 11 -- Container With Most Water
- leetcode(11) Container With Most Water
- 【Leetcode】Container With Most Water
- [Leetcode 11, Medium] Container With Most Water
- leetcode -- Container With Most Water -- 经典题目重要!
- [LeetCode]Container With Most Water, 解题报告
- LeetCode Container With Most Water
- Container With Most Water——LeetCode
- 【leetcode】Container With Most Water
- leetcode Container with most Water