Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cf62e5c

Browse files
update: 240
1 parent c0b901c commit cf62e5c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
119119
| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/solution/) | [JavaScript](./src/summary-ranges/res.js) | Medium |
120120
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/solution/) | [JavaScript](./src/product-of-array-except-self/res.js) | Medium |
121121
| 239 | [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/description/) | [JavaScript](./src/sliding-window-maximum/description/res.js) | Hard |
122-
| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [JavaScript](./src/search-a-2d-matrix-ii/res.js) | Medium |
122+
| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [JavaScript](./src/search-a-2d-matrix-ii/res.js) · [TypeScript](./src/search-a-2d-matrix-ii/res.ts) | Medium |
123123
| 274 | [H-Index](https://leetcode.com/problems/h-index/) | [JavaScript](./src/h-index/res.js) | Medium |
124124
| 275 | [H-Index II](https://leetcode.com/problems/h-index-ii/) | [JavaScript](./src/h-index-ii/res.js) | Medium |
125125
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [JavaScript](./src/first-bad-version/res.js) | Easy |

‎src/search-a-2d-matrix-ii/res.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function searchMatrix(matrix: number[][], target: number): boolean {
2+
const visitedMatrix: boolean[][] = [];
3+
4+
if (!matrix.length) {
5+
return false;
6+
}
7+
8+
const rowLength = matrix.length;
9+
const columnLength = matrix[0].length;
10+
11+
for (let i = 0; i < rowLength; i++) {
12+
visitedMatrix.push([]);
13+
for (let j = 0; j < columnLength; j++) {
14+
visitedMatrix[i].push(false);
15+
}
16+
}
17+
18+
const dfs = (i:number, j:number): boolean => {
19+
if (matrix[i][j] > target || visitedMatrix[i][j]) {
20+
return false;
21+
}
22+
23+
visitedMatrix[i][j] = true;
24+
if (matrix[i][j] === target) {
25+
return true;
26+
} else {
27+
return i < (rowLength - 1) && dfs(i+1, j) || j < (columnLength - 1) && dfs(i, j + 1);
28+
}
29+
}
30+
31+
return dfs(0, 0);
32+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /