当前位置:编程学习 > C/C++ >>

[LeetCode]Container With Most Water

[cpp
class Solution { 
//Find two lines, which together with x-axis forms a container, such that the container contains the most water.  
//if do not together with x-axis forms a container,   
//and together with other lines then it will be more difficult to find out an effective solution  
public: 
    int maxArea(vector<int> &height) { 
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        int l = 0; 
        int r = height.size()-1; 
        int ans = 0; 
        while (l < r) 
        { 
            int tmp = min(height[r], height[l])*(r-l); 
            if(tmp > ans) ans = tmp; 
            if(height[r] < height[l]) 
                r--; 
            else l++; 
        } 
        return ans; 
    } 
}; 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,