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 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.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
Analysis:
写出一个算法,能够找出一个m * n矩阵中是否含一个数。这个m*n的矩阵有如下性质:
1. 每行的整数从左到右是有序的;
2. 每行的第一个整数要比上一行的最后一个整数大。
思路:
不论是单行有序或整体有序,都可以使用这样的策略解决问题:
从矩阵的右上角开始寻找,如果target比他大,则往下找;如果target比他小,则往左找,如果矩阵中确实存在这个数,则一定能找到,若找到最后也没有找到,则返回false。
这道题目还可以用二分法解决。用两次二分法,先找到行,再找到列。(但是对于单行有序的题目来说则不适合解决)
Answer:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return false;
int row = matrix.length;
int col = matrix[0].length;
int i = 0;
int j = matrix[0].length-1;
while(i < matrix.length && j >= 0) {
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] < target)
i++;
else if(matrix[i][j] > target)
j--;
}
return false;
}
}
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.
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.
Analsyis:
这个题目与上面不同的是,矩阵的性质是:
1. 每行从走到有是有序的;
2. 每列从上到下是有序的;
但满足这两个性质并不能保证这个矩阵整体是有序的,因此用二分法一定不能解决问题,但是我们可以使用上面从右上角开始,更新i,j的值来寻找矩阵中是否存在这个元素的方法。
Answer:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return false;
int row = matrix.length;
int col = matrix[0].length;
int i = 0;
int j = matrix[0].length-1;
while(i < matrix.length && j >= 0) {
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] < target)
i++;
else if(matrix[i][j] > target)
j--;
}
return false;
}
}
LeetCode -- Search a 2D Matrix & Search a 2D Matrix II的更多相关文章
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [Leetcode] Binary search, Divide and conquer--240. 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
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [leetcode] Add to List 74. Search a 2D Matrix
/** * Created by lvhao on 2017/8/1. * Write an efficient algorithm that searches for a value in an m ...
- sorted matrix - search & find-k-th
sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual
monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...
随机推荐
- 转 IOS7开发错误收集
转自:http://blog.csdn.net/smallsky_keke/article/details/16117653 1. fatal error: file '/Applications/X ...
- react搭建一个框架(react-redux-axios-antd Designs)
废话不多说,先给一个github案例:前往.. 1.create-react-app <文件名> 安装router:npm i react-router-dom -S; npm inst ...
- java-访问控制修饰符
访问权限 public 任何情况都可以访问 默认包 本包范围内可以访问到 protect 同一个包里的所有类所可以访问:所有子类(子类可以不和父类在同一个包)都可以访问 privat ...
- MySQL数据库 : 查询语句,连接查询及外键约束
查询指定字段 select 字段1,字段2 from 表名; 消除重复行(重复指的是结果集中的所有完全重复行) select distinct 字段1,字段2.. ...
- python学习之控制流1
配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 控制流: # 1.布尔值: ...
- python -- 输出异常详细信息
在使用try: except: 捕获异常后,想要获取到异常信息的详细内容另做它用,可以使用python的内置模块traceback进行获取. traceback.print_exc() 直接打印异 ...
- C语言字符篇(五)内存函数
memcpy不可以把目的地址写成本身 但是memmove可以,因为它是先保存到临时空间 #include <string.h> void *memcpy(void *dest, con ...
- lambda & 三元运算
lambda & 三元运算 lambda: 1 >>> def add(x,y): #定义一个加法函数 2 return x+y ...
- Java重写与重载
重写的规则: 参数列表必须完全与被重写方法的相同: 返回类型必须完全与被重写方法的返回类型相同: 访问权限不能比父类中被重写的方法的访问权限更低.例如:如果父类的一个方法被声明为public,那么在子 ...
- 剑指Offer - 九度1515 - 打印1到最大的N位数
剑指Offer - 九度1515 - 打印1到最大的N位数2013-11-30 01:11 题目描述: 给定一个数字N,打印从1到最大的N位数. 输入: 每个输入文件仅包含一组测试样例.对于每个测试案 ...