Search a 2D Matrix | & II
Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.
This matrix has the following properties:
- Integers in each row are sorted from left to right.
- Integers in each column are sorted from up to bottom.
- No duplicate integers in each row or column.
Consider the following matrix:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
Given target = 3
, return 2
.
分析:
因为数组里的数有上面三个特性,所以我们可以从左下角开始找。如果当前值比target大,明显往上走,比target小,往右走,如果一样斜上走。Time complexity: (O(m + n)) (m = matrix.length, n = matrix[0].length)
public class Solution {
public int searchMatrix(int[][] matrix, int target) {
// check corner case
if (matrix == null || matrix.length == 0 || matrix[].length == 0) {
return ;
}
// from bottom left to top right
int x = matrix.length - ;
int y = ;
int count = ; while (x >= && y < matrix[].length) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {
count++;
x--;
y++;
}
}
return count; }
}
Search a 2D Matrix I
Write an efficient algorithm that searches for a value in an mx n matrix.
This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3
, return true
.
分析:
根据数组的特点,我们需要找出一个大范围(row),然后再确定小范围。
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == || matrix[].length == ) return false;
int rowCount = matrix.length, colCount = matrix[].length;
if (matrix[][] > target || matrix[rowCount - ][colCount - ] < target) return false; int row = getRowNumber(matrix, target);
int column = getColumnNumber(matrix, row, target); return matrix[row][column] == target; } public int getRowNumber(int[][] matrix, int target) {
int start = , end = matrix.length - , width = matrix[].length;
while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[mid][width - ] == target) {
return mid;
} else if (matrix[mid][width - ] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
} public int getColumnNumber(int[][] matrix, int row, int target) {
int start = , end = matrix[].length; while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[row][mid] == target) {
return mid;
} else if (matrix[row][mid] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
}
}
Search a 2D Matrix | & II的更多相关文章
- LintCode 38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- 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.该矩阵具有以下特性 ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- Session的异常
既然这一天就这么废了,那就多说一些吧!其实session也是有潜在的问题的.Session销毁的三种情况: (1)超时:超过30分钟 (2)服务器非正常关闭,如果自己手动stop service而不是 ...
- 【ZOJ 3480】Duck Typing
题 题意 1.有t组数据,输入时每组数据之间空格隔开,输出时也要求空格隔开. 2.每组都是一行begin开始,一行end结束. 3.class ClassName[:Super] 表示声明一个类型,S ...
- poj 3261 二分答案+后缀数组 求至少出现k次的最长重复子序列
#include "stdio.h" #define maxn 20010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int rank[ma ...
- Linux Rootkit Sample && Rootkit Defenser Analysis
目录 . 引言 . LRK5 Rootkit . knark Rootkit . Suckit(super user control kit) . adore-ng . WNPS . Sample R ...
- 增强型for循环,用于遍历数组元素
/** * */ package com.cn.u4; /** * @author Administrator *增强型for */ public class ZhengQiangFor { publ ...
- Centos目录结构详细版
使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...
- 织梦DedeCms网站更换域名后文章图片路径批量修改
因为织梦上传图片用的是绝对地址,如果域名更换后,之前发布的文章的图片URL是不会跟着改变的,所以我们需要把旧域名替换成新的域名,方法很简单,有一段SQL语句更新一下文章正文内容就行. 复制下面SQL语 ...
- PHP文件包含漏洞剖析
一. 什么才是”远程文件包含漏洞”?回答是:服务器通过php的特性(函数)去包含任意文件时,由于要包含的这个文件来源过滤不严,从而可以去包含一个恶意文件,而我们可以构造这个恶意文件来达到邪恶的目的. ...
- iOS6新特征:UICollectionView高级使用示例之CircleLayout
DEMO 下面再看看Demo运行的效果图,通过这样的一个Demo,我们可以看出,使用UICollectionView可以很方便的制作出照片浏览等应用.并且需要开发者写的代码也不多. 程序刚刚启 ...
- JavaScript 参数传递与变量复制
ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值. 基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 5 种基本数 ...