Skip to content

Commit 38f70f0

Browse files
committed
Add solution #240
1 parent 4322a0e commit 38f70f0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@
216216
237|[Delete Node in a Linked List](./0237-delete-node-in-a-linked-list.js)|Easy|
217217
238|[Product of Array Except Self](./0238-product-of-array-except-self.js)|Medium|
218218
239|[Sliding Window Maximum](./0239-sliding-window-maximum.js)|Hard|
219+
240|[Search a 2D Matrix II](./0240-search-a-2d-matrix-ii.js)|Medium|
219220
242|[Valid Anagram](./0242-valid-anagram.js)|Easy|
220221
257|[Binary Tree Paths](./0257-binary-tree-paths.js)|Easy|
221222
258|[Add Digits](./0258-add-digits.js)|Easy|
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 240. Search a 2D Matrix II
3+
* https://leetcode.com/problems/search-a-2d-matrix-ii/
4+
* Difficulty: Medium
5+
*
6+
* Write an efficient algorithm that searches for a value target in an m x n integer matrix
7+
* matrix. This matrix has the following properties:
8+
* - Integers in each row are sorted in ascending from left to right.
9+
* - Integers in each column are sorted in ascending from top to bottom.
10+
*/
11+
12+
/**
13+
* @param {number[][]} matrix
14+
* @param {number} target
15+
* @return {boolean}
16+
*/
17+
var searchMatrix = function(matrix, target) {
18+
for (let i = 0, j = matrix[0].length - 1; i < matrix.length && j >= 0;) {
19+
if (matrix[i][j] === target) {
20+
return true;
21+
}
22+
matrix[i][j] > target ? j-- : i++;
23+
}
24+
return false;
25+
};

0 commit comments

Comments
 (0)