You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内
画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,
使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。
思路
这题利用双指针去做,i 指向最左边,j 指向最右边。当发现左边比较高的时候,保持左边
不动,右边左移。当发现右边比较高的时候,保持右边不动,左边右移。在这个过程中不断
更新最大值,最后 i === j 的时候,即可求得最大值。
题解
letmaxArea=function(height){leti=0letj=height.length-1letmax=0while(i!==j){letleftHeight=height[i]letrightHeight=height[j]letx=j-iletareaif(leftHeight>rightHeight){area=rightHeight*xj--}else{area=leftHeight*xi++}max=area>max ? area : max}returnmax}
The text was updated successfully, but these errors were encountered:
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内
画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,
使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。
思路
这题利用双指针去做,i 指向最左边,j 指向最右边。当发现左边比较高的时候,保持左边
不动,右边左移。当发现右边比较高的时候,保持右边不动,左边右移。在这个过程中不断
更新最大值,最后 i === j 的时候,即可求得最大值。
题解
The text was updated successfully, but these errors were encountered: