日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

leetcode 11. Container With Most Water

發布時間:2025/3/16 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 11. Container With Most Water 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:題目大概意思就是讓你找兩個數使得兩個數的最小值乘以他們的距離最大Area

題解:設置兩個變量, 一個是從頭開始,一個是從尾開始,計算兩個數的Area,并更新最大值,如果開始的元素大于結尾的元素則尾部向前移動一個位置,否則頭部向后移動一個位置,循環下去直到連個變量相遇

?

#include <iostream> #include <vector> using namespace std;int maxArea(vector<int>& height){int left = 0;int right = height.size() - 1;int maxArea = 0;while(left < right){maxArea = max(maxArea, min(height[left], height[right]) * (right - left));if (height[left] > height[right])right --;elseleft ++;}return maxArea; }int main(){vector<int> height = {1, 8, 6, 2, 5, 4, 8, 3, 7};cout << maxArea(height) << endl; }

?

?

總結

以上是生活随笔為你收集整理的leetcode 11. Container With Most Water的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。