-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 11. 盛最多水的容器 | ||
|
||
[11. 盛最多水的容器](https://leetcode.cn/problems/container-with-most-water/description/) | ||
|
||
## 代码 | ||
|
||
::: code-group | ||
|
||
<<< ../../src/leetcode/containerWithMostWater/javascript.js{javascript} [javascript] | ||
<<< ../../src/leetcode/containerWithMostWater/typescript.ts{typescript} [typescript] | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 输入:[1,8,6,2,5,4,8,3,7] | ||
// 输出:49 | ||
|
||
export function maxArea(height) { | ||
if (!height.length || height.length === 1) return 0 | ||
let max = 0 | ||
let l = 0 | ||
let r = height.length - 1 | ||
max = (r - l) * Math.min(height[l], height[r]) | ||
while (l < r) { | ||
if (height[l] < height[r]) { | ||
l++ | ||
} else { | ||
r-- | ||
} | ||
const area = (r - l) * Math.min(height[l], height[r]) | ||
if (max < area) max = area | ||
} | ||
return max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 输入:[1,8,6,2,5,4,8,3,7] | ||
// 输出:49 | ||
|
||
// function maxArea(height: number[]): number { | ||
// if (!height.length || height.length === 1) return 0 | ||
// for (let i = 0; i < height.length; i++) { | ||
// for (let j = i + 1; j < height.length; j++) { | ||
// const area = (j - i) * Math.min(height[i], height[j]) | ||
// if (max < area) max = area | ||
// } | ||
// } | ||
// return max | ||
// } | ||
|
||
export function maxArea(height: number[]): number { | ||
if (!height.length || height.length === 1) return 0 | ||
let max = 0 | ||
let l = 0 | ||
let r = height.length - 1 | ||
max = (r - l) * Math.min(height[l], height[r]) | ||
while (l < r) { | ||
if (height[l] < height[r]) { | ||
l++ | ||
} else { | ||
r-- | ||
} | ||
const area = (r - l) * Math.min(height[l], height[r]) | ||
if (max < area) max = area | ||
} | ||
return max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// 输入:x = 121 | ||
// 输出:true | ||
|
||
export function isPalindrome(x) { | ||
if (x < 0) return false | ||
let num = x | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// 输入:x = 121 | ||
// 输出:true | ||
|
||
export function isPalindrome(x: number): boolean { | ||
if (x < 0) return false | ||
let num = x | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { expect, test } from 'vitest' | ||
import { maxArea } from '../src/leetcode/containerWithMostWater/typescript.ts' | ||
|
||
|
||
test(`[1,8,6,2,5,4,8,3,7]`, () => { | ||
expect(maxArea([1,8,6,2,5,4,8,3,7])).toBe(49) | ||
}) |