<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                >[success] # 11 盛最多水的容器 * **描述** 給定一個長度為 n 的整數數組?height?。有?n?條垂線,第 i 條線的兩個端點是?(i, 0)?和?(i, height[i])?。找出其中的兩條線,使得它們與?x?軸共同構成的容器可以容納最多的水。返回容器可以儲存的最大水量。 * **說明** 你不能傾斜容器。 ? * 示例 1: ![](https://img.kancloud.cn/86/5e/865ea140915862e281d93778d4b4820e_801x383.png) ~~~ 輸入:[1,8,6,2,5,4,8,3,7] 輸出:49 解釋:圖中垂直線代表輸入數組 [1,8,6,2,5,4,8,3,7]。在此情況下,容器能夠容納水(表示為藍色部分)的最大值為?49。 ~~~ 示例 2: ~~~ 輸入:height = [1,1] 輸出:1 ~~~ 來源:力扣(LeetCode) 鏈接:https://leetcode.cn/problems/container-with-most-water 著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。 >[info] ## 簡單粗暴雙重循環 * 雙重循環依次獲取兩個高度,高度取**二者最短**,寬度即為當前雙重循環**內外元素位置差** ~~~ /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { // 自定義一個最大值為0 let maxArea = 0; for(let i=0;i < height.length;i++ ){ for(let j = i;j< height.length;j++ ){ // 用最小的高度 * 內外循環坐標差的寬 const area = Math.min(height[j],height[i]) * (j-i) if(area > maxArea) maxArea = area } } return maxArea }; ~~~ >[info] ## 使用對撞指針思想 * 定義兩個指針,即當兩個指針組成的區域,兩個指針所在高度必定是最高的那個才有可能和別的位置值組合形成一個最大面積,因此兩個指針中需要將每次最短的那個指針進行替換和當前最高的進行組合 ![](https://img.kancloud.cn/22/cc/22cc36a54c48c310aae0d376960ed73f_846x449.png) >[danger] ##### js 代碼 ~~~ /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { let left = 0; let right = height.length -1; let maxArea = 0; while(left < right){ const leftH = height[left] const rightH = height[right] // 算出面積 const are = Math.min(leftH,rightH) * (right - left) // 獲取最大面積 if(are > maxArea) maxArea = are // 移動最短指向的指針 if(rightH > leftH){ left++ }else{ right -- } } return maxArea }; ~~~ >[danger] ##### java ~~~ class Solution { public int maxArea(int[] height) { int left = 0; int right = height.length - 1; int maxArea = 0; while(left < right){ int leftH = height[left]; int rightH = height[right]; // 獲取面積 int are = Math.min(leftH,rightH) * (right - left ); if(are > maxArea) maxArea = are; if(rightH < leftH){ right --; }else{ left ++; } } return maxArea; } } ~~~ >[danger] ##### 題解優化 * 如果當前最**短移動后的還沒有現在位置的高**,說明即使組合了也**不會超過之前高度**,因此優化思路就是**一直移動指針必須比之前高才算找對指針位置** * java ~~~ class Solution { public int maxArea(int[] height) { int l = 0,r = height.length - 1; int maxArea = 0; while(l < r){ int area = (r - l) * Math.min(height[l],height[r]); int minH = Math.min(height[l],height[r]); maxArea = Math.max(maxArea,area); while(height[l] <= minH && l < r){ l++; } while (height[r] <= minH && l < r){ r--; } } return maxArea; } } ~~~ * js ~~~ /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { let left = 0; let right = height.length -1; let maxArea = 0; while(left < right){ const minH = Math.min(height[left],height[right]) // 算出面積 const are = minH * (right - left) // 獲取最大面積 if(are > maxArea) maxArea = are // 移動最短指向的指針 while(height[left]<=minH && left<right){ left ++ } while(height[right]<=minH && left<right){ right -- } } return maxArea }; ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看