11. Container With Most Water

来源:互联网 发布:淘宝买刺刀 编辑:程序博客网 时间:2024/06/04 19:51

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 linei 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.

public class Solution {    public int maxArea(int[] height) {        int left = 0;int right = height.length-1;int max = 0,temp = 0;while(left<right) {if(height[left] > height[right]){temp = (right-left) * height[right];right--;}else{temp = (right-left) * height[left];left++;}max = max > temp ? max :temp;}return max;    }}//从两端开始遍历数组,每次计算结果,那边数值高,则缩进,以此寻找最小的

//从两端开始遍历数组,每次计算结果,那边数值高,则缩进,以此寻找最小的

0 0
原创粉丝点击