LeetCode——11. Container With Most Water
2017-01-23 09:34
197 查看
beats91%
算法思想,设一个left指针指向数组最左边和一个right指针指向数组最右边,计算他们之间的容量为height[left]和height[right]中较小者乘以宽度right-left,然后移动指针,移动指针后宽度变小,要使容量增大,必须height增加,所以移动height较小的指针,时间复杂度为O(N)
算法思想,设一个left指针指向数组最左边和一个right指针指向数组最右边,计算他们之间的容量为height[left]和height[right]中较小者乘以宽度right-left,然后移动指针,移动指针后宽度变小,要使容量增大,必须height增加,所以移动height较小的指针,时间复杂度为O(N)
public class Solution { public int maxArea(int[] height) { int left,right; int max=0; int curArea; int lowIndex; left=0;right=height.length-1; while(left<right){ int width=right-left; if(height[left]<=height[right]){ lowIndex=left; left++; } else{ lowIndex=right; right--; } curArea=height[lowIndex]*width; if(curArea>max) max=curArea; } return max; } }
相关文章推荐
- [LeetCode] Interleaving String
- LeetCode-Unique Binary Search Trees II
- Linked List Cycle II -- LeetCode
- [LeetCode] Search a 2D Matrix [25]
- LeetCode | Maximum Depth of Binary Tree(二叉树的深度)
- [C++]LeetCode: 6 Remove Duplicates from Sorted List II
- [LeetCode]154 Find Minimum in Rotated Sorted Array II
- 153.Find Minimum in Rotated Sorted Array [LeetCode Java实现]
- leetcode--Same Tree
- Valid Palindrome [leetcode]
- [Leetcode]#237 Delete Node in a Linked List
- LeetCode:Word Pattern
- Leetcode: Shortest Word Distance III
- [leetcode] 207. Course Schedule 解题报告
- LeetCode-235.Lowest Common Ancestor of a Binary Search Tree
- LeetCode 154. Find Minimum in Rotated Sorted Array II(旋转数组查找)
- [LeetCode] 24. Swap Nodes in Pairs
- [leetcode]89. Gray Code -- JavaScript 代码
- LeetCode 54. Spiral Matrix
- 个人记录-LeetCode 66. Plus One