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 ...
随机推荐
- Abstract_Factory
#include <iostream> using namespace std; #define DESTORY_POINTER(ptr) if (ptr) { delete ptr; p ...
- IOS_设置启动图片若干问题
在做项目时, 发现设置了LaunchImage时发现一些问题: 1. 启动图片的设置可以通过两种方法: 1) 通过在LaunchScreen里放入ImageView 并设置图片, 这种方法的好处在于不 ...
- phpStudy for Linux (lnmp+lamp一键安装包)
phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 phpStudy for Linux 支持Apache/Ng ...
- cookie工作原理
当客户访问某个基于PHP技术的网站时,在PHP中可以使用setcookie()函数生成一个cookie,系统经处理把这个cookie发送到客户端并保存在C:\Documents andSettings ...
- PHPcms 摘要
一 常量 /** * 主要定义了路径常量,项目中经常用到 **/ define('PHPCMS_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);// 项目 ...
- iphone document 图片存储和读取
转载自:http://longquan.iteye.com/blog/1669990 存: //此处首先指定了图片存取路径(默认写到应用程序沙盒 中) NSArray *paths = NSSearc ...
- Swift初步介绍
Swift是本届WWDC大会苹果推出的一门新开发语言,开发者网站上已经放出了这门新语言的介绍.教程和手册,如果手里有一台iOS设备的话,通过苹果的iBooks应用,从它的官方书店里搜索Swift,可以 ...
- JMS之开源实现ActiveMQ
1.ActiveMQ是开源的JMS实现. 可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给jms 服务端,而尽快的把屏幕返还给用户,且服务端能够多线程排队响应高并发的请求.可 ...
- MySQL 多实例数据库还原脚本-备份集与端口对应
版本:5.5.14 OS: ConetOS 6.3 1.创建recover.sh [root@yoon export]# vi recover.sh #!/bin/bash bakdir=/exp ...
- 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)
一般在使用视图进行查询时,视图中意般都关联了两个或者更多个表,一般在这种情况下才会使用视图,但是但我在使用视图来查询数据时没有问题,但是一旦在分页中使用到视图进行查询就会出现错误提示如下: ADODB ...