38. 搜索二维矩阵 II
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每一列的整数从上到下是排序的。
- 在每一行或每一列中没有重复的整数。
样例
考虑下列矩阵:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
给出target = 3,返回 2
挑战
要求O(m+n) 时间复杂度和O(1) 额外空间
class Solution {
public:
/**
* @param matrix: A list of lists of integers
* @param target: An integer you want to search in matrix
* @return: An integer indicate the total occurrence of target in the given matrix
*/
int searchMatrix(vector<vector<int>> &matrix, int target) {
// write your code here
if(matrix.size()<1) return 0;
int row=0;
int column=matrix[0].size()-1;
int result=0;
while(row<matrix.size() && column>=0)
{
if(matrix[row][column]<target)
{
row+=1;
}else if(matrix[row][column]>target)
{
column-=1;
}else
{
result++;
if(row<matrix.size()) row+=1;
else if(column>=0) column-=1;
else break;
}
}
return result;
}
};
########################################################################
class Solution:
"""
@param matrix: A list of lists of integers
@param target: An integer you want to search in matrix
@return: An integer indicate the total occurrence of target in the given matrix
"""
def searchMatrix(self, matrix, target):
# write your code here
if len(matrix)<1:
return 0
r=0
c=len(matrix[0])-1
res=0;
while r<len(matrix) and c>=0:
if matrix[r][c]>target:
c-=1
elif matrix[r][c]<target:
r+=1
else:
res+=1
if c>=0: c-=1
elif r<len(matrix): r+=-1
else: break
'''
if r<len(matrix): r+=1 # ok
elif c>=0: c-=1
else: break
'''
return res