博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Container With Most Water --装最多水的容器(重)
阅读量:4106 次
发布时间:2019-05-25

本文共 1794 字,大约阅读时间需要 5 分钟。

问题:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

解答:

代码:

class Solution {public:    int maxArea(vector
&height) { int lh,rh; int i,j; int temp,min; int area = 0; i = 0; j = height.size()-1; while(i < j) { if(height[i] < height[j]) { temp = height[i]*(j-i); ++i; } else { temp = height[j]*(j-i); --j; } if(temp > area) area = temp; } return area; }};
代码:

public class Solution {      public int maxArea(int[] height) {          int i, j, lh, rh, area, tmp, len = height.length;            lh = height[0];          rh = height[len - 1];          area = 0;          i = 0;          j = len - 1;                    while (i < j) {              tmp = Math.min(lh, rh) * (j - i);                            if (tmp > area) {                  area = tmp;              }                            if (lh < rh) {                  while (i < j && height[i] <= lh) {                      i ++;                  }                  if (i < j) {                      lh = height[i];                  }              } else {                  while (i < j && height[j] <= rh) {                      j --;                  }                  if (i < j) {                      rh = height[j];                  }              }          }            return area;      }  }

转载地址:http://cktsi.baihongyu.com/

你可能感兴趣的文章
error C2065: “CString”: 未声明的标识符
查看>>
Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version~~~~
查看>>
error C2668: “pow”: 对重载函数的调用不明确
查看>>
详解C语言字节对齐
查看>>
Long Long、__int64使用总结
查看>>
c语言内存分配函数
查看>>
c语言内存分配函数之间的区别
查看>>
二维数组和指针的一些感悟
查看>>
二维数组和二级指针
查看>>
VC让对话框显示就最大化
查看>>
Unicode和多字节字符集 (MBCS) 杂谈
查看>>
CString GetBuffer() 与releasebuffer()的使用
查看>>
CString ,BSTR ,LPCTSTR之间关系和区别
查看>>
vs2008无法执行添加/移除操作,因为代码元素**是只读的
查看>>
创建定时器SetTimer(1,1000,NULL)
查看>>
GDI绘图
查看>>
[VC++6.0]如何设置和获取IP地址控件的内容
查看>>
VC++多线程编程
查看>>
VC 线程通信的3种方法
查看>>
typedef __packed struct
查看>>