题目

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. [
  2. [1, 3, 5, 7],
  3. [10, 11, 16, 20],
  4. [23, 30, 34, 50]
  5. ]

Given target = 3, return true.

原题链接(点我)

解题思路

二维数组中的查找,这是个简单的题,依据题意能够推出,这个二维数组事实上是一个有序的一维数组。解决思路也非常easy想到,每次比較每一维最后一个元素,假设该元素比要找的元素小,说明这个行不可能含该元素;假设相等,那就找到了,假设最后一个元素比要找元素大,说明该元素假设出现比在这一行。然后再在这一行中即可查找(能够用顺序,也可二分)。

代码实现

  1. class Solution {
  2. public:
  3. bool searchMatrix(vector<vector<int> > &matrix, int target) {
  4. int m = matrix.size();
  5. if(m<=0) return false;
  6. int n = matrix[0].size();
  7. for(int i=0; i<m; ++i){
  8. if(matrix[i][n-1] == target)
  9. return true;
  10. else if(matrix[i][n-1] < target)
  11. continue;
  12. else{
  13. for(int j=0; j<n; ++j)
  14. if(matrix[i][j] == target)
  15. return true;
  16. return false;
  17. }
  18. }
  19. return false;
  20. }
  21. };
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29564491
)

[LeetCode] Search a 2D Matrix [25]的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  4. LeetCode: Search a 2D Matrix 解题报告

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  5. 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 ...

  6. 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 ...

  7. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. HibernateDaoSupport的getSession()与HibernateTemplate的区别

    在 Spring+Hibernate的集成环境里,如果DAO直接使用HibernateDaoSupport的getSession()方法获取 session进行数据操作而没有显式地关闭该session ...

  2. POJ 2976 Dropping tests(二分答案)

    [题目链接]  http://poj.org/problem?id=2976 [题目大意] 给出每门成绩的总分和得分,去除k门成绩之后 使得剩余的成绩分数和除以总分得到的数字最大,要求精度在三位小数之 ...

  3. java 为pdf添加水印图片

    首先需要引入两个Jar包分别是:iTextAsian.jar .itext-2.1.7.jar  可以去  http://download.csdn.net/detail/work201003/922 ...

  4. jquery中read与js中onload区别

    在JavaScript中,onload函数是最经常使用的,几乎涉及到JavaScript的童鞋都少不了要接触它.这个函数的作用就是等待网页完全装载完了以后再去执行代码块内的语句,因为按照文档流的执行顺 ...

  5. 兼容所有浏览器的CSS3圆角

    兼容所有浏览器的CSS3圆角      解决CSS3圆角兼容所有浏览器的方法.本文提到了一种很不错的实现跨浏览器圆角的解决方案,但是说的不够全面,前端观察最近将整理更多更全面的资源给大家,敬请期待. ...

  6. HBASE学习笔记--API

    HBaseConfiguration HBaseConfiguration是每一个hbase client都会使用到的对象,它代表的是HBase配置信息.它有两种构造方式: public HBaseC ...

  7. NodeJS下载文件实例

    var http = require('http');var express = require('express');var fs=require("fs"); var app ...

  8. android开发SDcard 响应的文件相关处理(一)

    android开发相关文件类的处理工具类: package com.gzcivil.utils; import java.io.File; import java.util.ArrayList; im ...

  9. CDH(cdh5.7) 上集成 kafka

    CDH 可以在线下载: 离线安装

  10. C++ 数据结构学习一(顺序表)

    //SequentialList.h 顺序表模板类 #ifndef SEQUENTIAL_LIST_HXX#define SEQUENTIAL_LIST_HXX using std::cout; us ...