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.


题解:每次用右上角的元素跟target比较,有三种情况:

  1. 相等,说明找到了target;
  2. 右上角元素比target元素大,那么说明target在第一行,递归的搜索第一行。
  3. 右上角元素比target元素小,那么说明target在第二行及以后,递归的搜索如下图所示区域:

代码如下:

 public class Solution {
private boolean IfFind = false;
private void RightCornerRecur(int[][] matrix,int target,int m_start,int m_end,int n_start,int n_end){
if(m_start > m_end || n_start > n_end)
return; if(m_start < 0 || n_start < 0 || m_end >= matrix.length || n_end >= matrix[0].length)
return; if(matrix[m_start][n_end] == target){
IfFind = true;
return;
}
if(matrix[m_start][n_end] > target)
RightCornerRecur(matrix, target, m_start, m_start, n_start, n_end-1);
else {
RightCornerRecur(matrix, target, m_start+1,m_end, n_start, n_end);
} }
public boolean searchMatrix(int[][] matrix, int target) {
RightCornerRecur(matrix, target,0,matrix.length-1, 0, matrix[0].length-1);
return IfFind;
}
}

右上角可以用来比较剪掉一些元素,左下角同样可以,下面的代码中加上了左下角元素与target元素的比较,最终的运行时间是384ms,而上述只考虑右上角的运行时间是472ms。

 public class Solution {
private boolean IfFind = false;
private void RightCornerRecur(int[][] matrix,int target,int m_start,int m_end,int n_start,int n_end){ if(m_start > m_end || n_start > n_end)
return; if(m_start < 0 || n_start < 0 || m_end >= matrix.length || n_end >= matrix[0].length)
return; if(matrix[m_start][n_end] == target){
IfFind = true;
return;
} if(matrix[m_start][n_end] > target)
RightCornerRecur(matrix, target, m_start, m_start, n_start, n_end-1); else {
if(matrix[m_end][0] == target){
IfFind = true;
return;
}
if(matrix[m_end][0] < target)
RightCornerRecur(matrix, target, m_end, m_end, n_start+1, n_end);
else
RightCornerRecur(matrix, target, m_start+1,m_end-1, n_start, n_end);
} }
public boolean searchMatrix(int[][] matrix, int target) {
RightCornerRecur(matrix, target,0,matrix.length-1, 0, matrix[0].length-1);
return IfFind;
}
}

【leetcode刷题笔记】Search a 2D Matrix的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. Leetcode 74 and 240. Search a 2D matrix I and II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  4. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  5. 【leetcode刷题笔记】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  7. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. 【leetcode刷题笔记】Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. 【leetcode刷题笔记】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. vue实践---vue配合express实现请求数据mock

    mock数据是前端比较常见的技术,这里介绍下vue配合express 实现请求数据mock. 第一步: 安装 express :  npm install express -D 第二步: 简历需要mo ...

  2. vue实践---vue不依赖外部资源实现简单多语

    vue使用多语,最常见的就是 vue-i18n, 但是如果开发中的多语很少,比如就不到10个多语,这样就没必要引入vue-i18n了, 引入了反正导致代码体积大了,这时候单纯用vue实现多语就是比较好 ...

  3. LINQ TO SQL:操作有层次关系的对象

    对于关系型数据与对象数据之间最大的隔阂就是由标识列连接起来的行(关系型数据)与由集合保存的对象(对象数据)之间的冲突. 例如某个Subject对象(也就是数据库中的Subject表),从Subject ...

  4. python函数-------python2.7教程学习【廖雪峰版】(三)

    任务: 看完函数这一章    已完成 2017年6月8日16:23:491.函数的作用:写较少的代码实现较多的功能,可以多次被调用.2.可见,借助抽象,我们才能不关心底层的具体计算过程,而直接在更高的 ...

  5. Laravel开发:Laravel核心——Ioc服务容器

    服务容器 在说 Ioc 容器之前,我们需要了解什么是 Ioc 容器. Laravel 服务容器是一个用于管理类依赖和执行依赖注入的强大工具. 在理解这句话之前,我们需要先了解一下服务容器的来龙去脉:  ...

  6. 大数进制转换 poj1220

    普通的做法,大数除小数. 复杂度o( log(n)*log(n) ),其实就是位数的平方. NUMBER BASE CONVERSION Time Limit: 1000MS   Memory Lim ...

  7. excel生成随机数

    这个功能可以通过excel来实现,操作步骤如下:       1.新建一个excel,并打开       2.选中一个单元格,在单元格中填写:    =20*RAND()+30  确定之后就会发现已经 ...

  8. 记录-在jsp页面获取后台值在页面显示过长处理

    在下面的红色标记处 后台获取的值(字符串)在页面显示过长或者与其他重叠 (xxx).cutStr(15) 15代表的是展示字符串的长度 data.rows[i].avgPrice, ), data.r ...

  9. 基于SpringMVC国际化资源配置方式

    1.首先需要在spring-mvc-servlet.xml 中配置拦截器: <bean id="localeChangeInterceptor" class="or ...

  10. node-sass 安装失败的解决措施[转]

    转自:http://blog.csdn.net/nzb329/article/details/51935236 在编译一个项目的时候,一直报错 后来发现是因为node-sass没有装成功, 最终的解决 ...