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 4ad4079

Browse files
add: Search a 2D Matrix II
1 parent 0d0e187 commit 4ad4079

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Progress: 20/
2828
|196|[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/) | [SQL](./src/delete-duplicate-emails/res.txt)|Easy|
2929
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt)|Easy|
3030
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [JavaScript](./src/reverse-linked-list/res.js)|Easy|
31+
|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|
3132
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [JavaScript](./src/power-of-four/res.js)|Easy|
3233
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [JavaScript](./src/reverse-string/res.js)|Easy|
3334
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [JavaScript](./src/sum-of-left-leaves/res.js)|Easy|

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
2+
3+
// Integers in each row are sorted in ascending from left to right.
4+
// Integers in each column are sorted in ascending from top to bottom.
5+
// For example,
6+
7+
// Consider the following matrix:
8+
9+
// [
10+
// [1, 4, 7, 11, 15],
11+
// [2, 5, 8, 12, 19],
12+
// [3, 6, 9, 16, 22],
13+
// [10, 13, 14, 17, 24],
14+
// [18, 21, 23, 26, 30]
15+
// ]
16+
// Given target = 5, return true.
17+
18+
// Given target = 20, return false.
19+
20+
/**
21+
* res.js
22+
* @authors Joe Jiang (hijiangtao@gmail.com)
23+
* @date 2017年02月26日 22:12:39
24+
* @version $Id$
25+
*
26+
*
27+
* @param {number[][]} matrix
28+
* @param {number} target
29+
* @return {boolean}
30+
*/
31+
let searchMatrix = function(matrix, target) {
32+
// Nothing in the matrix
33+
if (matrix.length === 0 || matrix[0] === 0) {
34+
return false;
35+
}
36+
37+
let rowlen = matrix.length,
38+
collen = matrix[0].length;
39+
40+
let i = 0, j = collen-1;
41+
while (i>=0 && i<rowlen && j>=0 && j<collen) {
42+
if (matrix[i][j] === target) {
43+
return true;
44+
} else if (matrix[i][j] > target) {
45+
j -= 1;
46+
} else {
47+
i += 1;
48+
}
49+
}
50+
51+
return false;
52+
};

0 commit comments

Comments
(0)

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