-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
011.Container With Most Water (java) #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution { | ||
| public int maxArea(int[] height) { | ||
| int start = 0, end = height.length - 1, maxArea = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 虽然题目没有说明是否有非空的情况,但如果从程序健壮性/实际应用的角度来看,加以下一小段代码是有必要的噢~ if (height == null || height.length < 2) { return 0; } 否则当传入 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. image There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| while (start < end) { | ||
| int hs = height[start]; | ||
| int he = height[end]; | ||
| int l = end - start; | ||
| if (hs > he) { | ||
| maxArea = Math.max(he * l, maxArea); | ||
| end--; | ||
| } else { | ||
| maxArea = Math.max(hs * l, maxArea); | ||
| start++; | ||
| } | ||
| } | ||
| return maxArea; | ||
| } | ||
| } | ||