Skip to content

Commit ac8f475

Browse files
authored
feat: add ts solution to lc problem: No.0240.Search a 2D Matrix II (doocs#501)
1 parent 5bbdd44 commit ac8f475

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

solution/0200-0299/0240.Search a 2D Matrix II/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ class Solution {
9595
}
9696
```
9797

98+
### **TypeScript**
99+
100+
```ts
101+
function searchMatrix(matrix: number[][], target: number): boolean {
102+
let m = matrix.length, n = matrix[0].length;
103+
let i = m - 1, j = 0;
104+
while (i >= 0 && j < n) {
105+
let cur = matrix[i][j];
106+
if (cur == target) return true;
107+
if (cur > target) {
108+
--i;
109+
} else {
110+
++j;
111+
}
112+
}
113+
return false;
114+
};
115+
```
116+
98117
### **C++**
99118

100119
```cpp

solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ class Solution {
8383
}
8484
```
8585

86+
### **TypeScript**
87+
88+
```ts
89+
function searchMatrix(matrix: number[][], target: number): boolean {
90+
let m = matrix.length, n = matrix[0].length;
91+
let i = m - 1, j = 0;
92+
while (i >= 0 && j < n) {
93+
let cur = matrix[i][j];
94+
if (cur == target) return true;
95+
if (cur > target) {
96+
--i;
97+
} else {
98+
++j;
99+
}
100+
}
101+
return false;
102+
};
103+
```
104+
86105
### **C++**
87106

88107
```cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function searchMatrix(matrix: number[][], target: number): boolean {
2+
let m = matrix.length, n = matrix[0].length;
3+
let i = m - 1, j = 0;
4+
while (i >= 0 && j < n) {
5+
let cur = matrix[i][j];
6+
if (cur == target) return true;
7+
if (cur > target) {
8+
--i;
9+
} else {
10+
++j;
11+
}
12+
}
13+
return false;
14+
};

0 commit comments

Comments
 (0)