LeetCode【11】 盛水最多的容器

题目:

分析:

1、双指针,储水为(R-L )* 二者较小高度,如题目,(9-2)* 7 = 49

2、双指针向中间靠,每次移动较矮的指针。

代码:

java 复制代码
public int maxArea(int[] height) {
     int left = 0;
     int right = height.length - 1;
     int max = 0;
     while (left < right) {
         if (height[right] > height[left]) {
             max = Math.max(max, (right - left) * height[left]);
             left++;
         } else {
             max = Math.max(max, (right - left) * height[right]);
             right--;
         }
     }

     return max;
 }
相关推荐
To_OC2 小时前
LC 15 三数之和:双指针不难,难的是把去重做对
javascript·算法·leetcode
阿维的博客日记4 小时前
MultipartFile 是不是表示仅仅是一个分片?
java·后端·spring·multipartfile
renhongxia14 小时前
世界模型,是“空中楼阁”还是AGI的“最后一块拼图”?
运维·服务器·数据库·人工智能·算法·agi
程序员无隅4 小时前
Coding Agent 为什么压缩上下文后还能继续工作?上下文模块设计拆解
java·开发语言·数据库
Python+995 小时前
Java 枚举类(Enum)详解:从基础到高级应用
java·开发语言·python
G.O.G.O.G5 小时前
LeetCode SQL 从入门到精通(MySQL)06(上)
数据库·sql·mysql·leetcode
二炮手亮子5 小时前
浅记java线程池
java·开发语言
一路向北North5 小时前
Spring Security OAuth2.0(23):分布式系统授权-转发明文给微服务
java·spring·微服务
zephyr056 小时前
动态规划-最长上升子序列问题
算法·动态规划
闪电悠米7 小时前
力扣hot100-56.合并区间-排序详解
数据结构·算法·leetcode·贪心算法·排序算法