lintcode:搜索二维矩阵II
题目
搜索二维矩阵 II
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每一列的整数从上到下是排序的。
- 在每一行或每一列中没有重复的整数。
考虑下列矩阵:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
给出target = ,返回 2
要求O(m+n) 时间复杂度和O(1) 额外空间
解题
直接遍历,时间复杂度是O(MN)
public class Solution {
/**
* @param matrix: A list of lists of integers
* @param: A number you want to search in the matrix
* @return: An integer indicate the occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix == null)
return 0;
int row = matrix.length;
if(row ==0)
return 0;
int col = matrix[0].length;
int count =0;
for(int i=0;i< row ;i++){
for(int j=0;j<col;j++){
if(matrix[i][j] == target)
count++;
}
}
return count;
}
}
Java Code
题目中数组是有序的,并且每一行或者每一列的元素没有重复的
可以发现,某个数出现的次数在 0 到Min(col,row) 之间
剑指offer上好像有一个和这个很类似的题目
通过对矩阵进行划分,一次去一列
Java程序
public class Solution {
/**
* @param matrix: A list of lists of integers
* @param: A number you want to search in the matrix
* @return: An integer indicate the occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix == null)
return 0;
int row = matrix.length;
if(row ==0)
return 0;
int col = matrix[0].length;
int count =0;
int i=0;
int j=col-1;
while(i<row && j>=0){
if(matrix[i][j] > target){
j--;
}else if(matrix[i][j]< target){
i++;
}else{
count++;
i++;
j--;
}
}
return count;
} }
Java Code
Python程序
class Solution:
"""
@param matrix: An list of lists of integers
@param target: An integer you want to search in matrix
@return: An integer indicates the total occurrence of target in the given matrix
"""
def searchMatrix(self, matrix, target):
# write your code here
if matrix == None:
return 0
row = len(matrix)
if row ==0:
return 0
col = len(matrix[0])
count = 0
i = 0
j = col - 1
while i<row and j>=0:
if matrix[i][j] > target:
j -=1
elif matrix[i][j] < target:
i += 1
else:
count += 1
i += 1
j -= 1
return count
Python Code
lintcode:搜索二维矩阵II的更多相关文章
- LintCode-38.搜索二维矩阵 II
搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复 ...
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- leetcode-240搜索二维矩阵II
搜索二维矩阵II class Solution: def searchMatrix(self, matrix, target): """ :type matrix: Li ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- Java实现 LeetCode 240 搜索二维矩阵 II(二)
240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...
- lintcode :搜索二维矩阵
题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...
- 240. 搜索二维矩阵 II
二维数组搜索 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
- leetcode240 搜索二维矩阵 II
题目: 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ma ...
随机推荐
- WinForm TreeView节点重绘,失去焦点的高亮显示
当用户焦点离开TreeView时,TreeView选中节点仍然高亮,但是颜色符合主题. 设置TreeView.HideSelection = False;可让选中节点保持高亮. 添加重绘事件 Tree ...
- [大牛翻译系列]Hadoop(14)MapReduce 性能调优:减小数据倾斜的性能损失
6.4.4 减小数据倾斜的性能损失 数据倾斜是数据中的常见情况.数据中不可避免地会出现离群值(outlier),并导致数据倾斜.这些离群值会显著地拖慢MapReduce的执行.常见的数据倾斜有以下几类 ...
- linux查看硬件信息的命令(图文)
发布:脚本学堂/Linux命令 编辑:JB02 2013-12-23 21:48:18 [大 中 小] 转自:http://www.jbxue.com/LINUXjishu/14996.htm ...
- Translation002—Package Index(Android包索引)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 看本翻译前请您注意: 本人初学android,可能有的翻译不是非常准确,但本人尽最大努力,不清楚处会做标记,并附 ...
- Lambda(2)
Lambda表达式是匿名方法的超集,处理匿名方法有的功能外,还有其他的功能: 1.能够推测出参数的类型,无需显示声明 2.支持语句块和表达式作为方法体 Lambda表达式的书写方式: Lambda表达 ...
- Windows7 sp1 64位下安装配置eclipse+jdk+CDT+minGW
需要的工具: jdk-7u11-windows-x64.exe eclipse-SDK-4.2.2-win32-x86_64.zip cdt-master-8.1.2.zip mingw-get-i ...
- Sales_item
#ifndef SALESITEM_H #define SALESITEM_H // Definition of Sales_item class and related functions goes ...
- matlab实现插值法sin函数
插值法实现sin函数: %calculate and print the sine function %input: x %output: sin(x) similar function y = si ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 用MSBuild和Jenkins搭建持续集成环境 - 转
http://www.infoq.com/cn/articles/MSBuild-1 http://www.infoq.com/cn/articles/MSBuild-2 MSBuild是在.NET ...