240. Search a 2D Matrix II

  • 38.0%

https://leetcode.com/problems/search-a-2d-matrix-ii/#/description

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
For example,

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.

Given target = 20, return false.

方法一:

剑指offer 3

我的代码实现:

从右上角开始搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
// int m = matrix.size();
// if(m==0) return false;
// int n = matrix[0].size();
// if(n==0) return false;
if(matrix.size()==0 || matrix[0].size()==0)
return false;
int m=matrix.size(), n=matrix[0].size();
if(target<matrix[0][0] || target>matrix[m-1][n-1]) return false;
int i=0, j=n-1;
while(i<m && j>=0){
if(target==matrix[i][j])
return true;
else if(target<matrix[i][j])
j--;
else
i++;
}
return false;
}
};

注意代码中

1
2
3
if(matrix.size()==0 || matrix[0].size()==0)
return false;
int m=matrix.size(), n=matrix[0].size();

替代

1
2
3
4
int m = matrix.size();
if(m==0) return false;
int n = matrix[0].size();
if(n==0) return false;

对于矩阵是好用的


184ms, 78.57%, June.23th, 2016

https://leetcode.com/discuss/47528/c-with-o-m-n-complexity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if(m == 0) return false;
int n = matrix[0].size();

int i = 0, j = n - 1;
while(i < m && j >= 0){
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] > target)
j--;
else
i++;
}
return false;
}
};

Solution mine:

720ms, 10.08%, June.23th, 2016

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
for i in xrange(len(matrix)):
for j in xrange(len(matrix[0])):
if matrix[i][j] == target:
return True
return False

Solution 1:

184ms, 40.60%, June.23th, 2016

https://leetcode.com/discuss/47571/4-lines-c-6-lines-ruby-7-lines-python-1-liners

1
2
3
4
5
6
7
8
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
return any(target in row for row in matrix)

Solution 2:

124ms, 59.95%, June.23th, 2016

https://leetcode.com/discuss/47571/4-lines-c-6-lines-ruby-7-lines-python-1-liners

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
j = -1
for row in matrix:
while j + len(row) and row[j] > target:
j -= 1
if row[j] == target:
return True
return False

13ms, 53.66%, June.23th, 2016

https://leetcode.com/discuss/48852/my-concise-o-m-n-java-solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length < 1 || matrix[0].length < 1)
return false;

int col = matrix[0].length - 1;
int row = 0;
while(col >= 0 && row <= matrix.length - 1){
if(target == matrix[row][col])
return true;
else if(target < matrix[row][col])
col--;
else
row++;
}
return false;
}
}
谢谢你,可爱的朋友。