C++實(shí)現(xiàn)LeetCode(11.裝最多水的容器)
[LeetCode] 11. Container With Most Water 裝最多水的容器
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 line i 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 and nis at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
這道求裝最多水的容器的題和那道 Trapping Rain Water 很類似,但又有些不同,那道題讓求整個(gè)能收集雨水的量,這道只是讓求最大的一個(gè)的裝水量,而且還有一點(diǎn)不同的是,那道題容器邊緣不能算在里面,而這道題卻可以算,相比較來(lái)說(shuō)還是這道題容易一些,這里需要定義i和j兩個(gè)指針分別指向數(shù)組的左右兩端,然后兩個(gè)指針向中間搜索,每移動(dòng)一次算一個(gè)值和結(jié)果比較取較大的,容器裝水量的算法是找出左右兩個(gè)邊緣中較小的那個(gè)乘以兩邊緣的距離,代碼如下:
C++ 解法一:
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0, i = 0, j = height.size() - 1;
while (i < j) {
res = max(res, min(height[i], height[j]) * (j - i));
height[i] < height[j] ? ++i : --j;
}
return res;
}
};
Java 解法一:
public class Solution {
public int maxArea(int[] height) {
int res = 0, i = 0, j = height.length - 1;
while (i < j) {
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
if (height[i] < height[j]) ++i;
else --j;
}
return res;
}
}
這里需要注意的是,由于 Java 中的三元運(yùn)算符 A?B:C 必須須要有返回值,所以只能用 if..else.. 來(lái)替換,不知道 Java 對(duì)于三元運(yùn)算符這么嚴(yán)格的限制的原因是什么。
下面這種方法是對(duì)上面的方法進(jìn)行了小幅度的優(yōu)化,對(duì)于相同的高度們直接移動(dòng)i和j就行了,不再進(jìn)行計(jì)算容量了,參見代碼如下:
C++ 解法二:
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0, i = 0, j = height.size() - 1;
while (i < j) {
int h = min(height[i], height[j]);
res = max(res, h * (j - i));
while (i < j && h == height[i]) ++i;
while (i < j && h == height[j]) --j;
}
return res;
}
};
Java 解法二:
public class Solution {
public int maxArea(int[] height) {
int res = 0, i = 0, j = height.length - 1;
while (i < j) {
int h = Math.min(height[i], height[j]);
res = Math.max(res, h * (j - i));
while (i < j && h == height[i]) ++i;
while (i < j && h == height[j]) --j;
}
return res;
}
}
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(11.裝最多水的容器)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)裝最多水的容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字)
- C++實(shí)現(xiàn)LeetCode(55.跳躍游戲)
- C++實(shí)現(xiàn)LeetCode(45.跳躍游戲之二)
- C++實(shí)現(xiàn)LeetCode(769.可排序的最大塊數(shù))
- C++實(shí)現(xiàn)LeetCode(768.可排序的最大塊數(shù)之二)
- C++實(shí)現(xiàn)LeetCode(84.直方圖中最大的矩形)
- C++實(shí)現(xiàn)LeetCode(42.收集雨水)
- C++實(shí)現(xiàn)LeetCode(13.羅馬數(shù)字轉(zhuǎn)化成整數(shù))
相關(guān)文章
c++中string類成員函數(shù)c_str()的用法
c_str()函數(shù)返回一個(gè)指向正規(guī)c字符串的指針,內(nèi)容和string類的本身對(duì)象是一樣的,通過(guò)string類的c_str()函數(shù)能夠把string對(duì)象轉(zhuǎn)換成c中的字符串的樣式2013-09-09
c語(yǔ)言結(jié)構(gòu)體字節(jié)對(duì)齊的實(shí)現(xiàn)方法
在c語(yǔ)言的結(jié)構(gòu)體里面一般會(huì)按照某種規(guī)則去進(jìn)行字節(jié)對(duì)齊。本文就來(lái)介紹一下如何實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解下2021-07-07
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開始了)[三]
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開始了)[三]...2007-02-02
visual studio code 配置C++開發(fā)環(huán)境的教程詳解 (windows 開發(fā)環(huán)境)
這篇文章主要介紹了 windows 開發(fā)環(huán)境下visual studio code 配置C++開發(fā)環(huán)境的圖文教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C語(yǔ)言設(shè)計(jì)實(shí)現(xiàn)掃描器的自動(dòng)機(jī)的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言設(shè)計(jì)實(shí)現(xiàn)掃描器的自動(dòng)機(jī),可識(shí)別的單詞包括:關(guān)鍵字、界符、標(biāo)識(shí)符和常整型數(shù),感興趣的小伙伴可以了解一下2022-12-12
剖析C語(yǔ)言關(guān)鍵字之void,const,return
這篇文章主要為大家介紹了C語(yǔ)言關(guān)鍵字之void,const,return,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01

