首页 > 代码库 > LeetCode--Container With Most Water

LeetCode--Container With Most Water

双指针

 1 class Solution { 2 public: 3     int maxArea(vector<int> &height) { 4         if(height.size() == 0 || height.size() == 1) 5             return 0; 6         int maxarea=(height.size()-1)*(height[0] > height[height.size()-1] ? height[height.size()-1] : height[0]); 7         int i=0; 8         int j=height.size()-1; 9         while(i<=j)10         {11             int curarea=(j-i)*(height[i] > height[j] ? height[j] : height[i]);12             if(height[i]<height[j])13             {14                 i++;15             }16             else17             {18                 --j;19             }20             maxarea=(maxarea > curarea) ? maxarea : curarea;21         }22         return maxarea;23     }24 };

 

LeetCode--Container With Most Water