functionclass[LeetCode]Container With Most Water

本文介绍了一种寻找能盛最多水的两条线段的有效算法。通过对数组的高度进行双指针遍历,逐步缩小搜索范围,实现了高效求解。文章还分享了程序员幽默语录,增添阅读趣味。

在写这篇文章之前,xxx已经写过了几篇关于改functionclass主题的文章,想要了解的朋友可以去翻一下之前的文章

    每日一道理
这浓浓的母爱使我深深地认识到:即使你是一只矫健的雄鹰,也永远飞不出母爱的长空;即使你是一条扬帆行驶的快船,也永远驶不出母爱的长河!在人生的路上不管我们已走过多远,还要走多远,我们都要经过母亲精心营造的那座桥!
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;
	}
};

文章结束给大家分享下程序员的一些笑话语录: 一个合格的程序员是不会写出 诸如 “摧毁地球” 这样的程序的,他们会写一个函数叫 “摧毁行星”而把地球当一个参数传进去。

--------------------------------- 原创文章 By
function和class
---------------------------------

转载于:https://www.cnblogs.com/jiangu66/archive/2013/05/28/3105096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值