Leetcode 11. 盛最多水的容器
2023-06-13 09:15:27 时间
算法:左右双指针。两端中的短边朝向另一端移动,直至双指针相遇。
时间复杂度:O(N)
我的C源码:
int maxArea(int* height, int heightSize){
int max = 0;
int left= 0;
int right = heightSize-1;
int a, b, area;
while(left < right)
{
a = height[left];
b = height[right];
if (a <= b)
{
area = a * (right - left++);
}
else
area = b * (right-- - left);
if(area > max) max = area;
}
// for(int i =0; i< heightSize-1; i++) //暴力解法会超时
// {
// for(int j= i+1; j< heightSize; j++)
// {
// h = height[i] <= height[j] ? height[i] : height[j];
// area = h * (j-i);
// if(area > max) max = area;
// }
// }
return max;
相关文章
- leetcode 2. 两数相加 js 实现
- ☆打卡算法☆LeetCode 205. 同构字符串 算法解析
- ☆打卡算法☆LeetCode 206. 反转链表 算法解析
- ☆打卡算法☆LeetCode 214. 最短回文串 算法解析
- leetcode-剑指offer59-I 滑动窗口的最大值
- LeetCode刷题系列(4)
- ☆打卡算法☆LeetCode 201. 数字范围按位与 算法解析
- LeetCode 551. 学生出勤记录 I
- 最小栈(leetcode 155)
- JavaScript刷LeetCode拿offer-双指针技巧Medium篇
- 【day09】LeetCode(力扣)每日一刷[1640. 能否连接形成数组 ][102. 二叉树的层序遍历 ][704. 二分查找 ]
- leetcode 链表初探 21. merge two sorted lists
- LeetCode-32-最长有效括号
- 每日一道leetcode:11. 盛最多水的容器
- leetcode 71. 简化路径