[LeetCode] Search a 2D Matrix [25]
题目
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
.
解题思路
二维数组中的查找,这是个简单的题,依据题意能够推出,这个二维数组事实上是一个有序的一维数组。解决思路也非常easy想到,每次比較每一维最后一个元素,假设该元素比要找的元素小,说明这个行不可能含该元素;假设相等,那就找到了,假设最后一个元素比要找元素大,说明该元素假设出现比在这一行。然后再在这一行中即可查找(能够用顺序,也可二分)。
代码实现
- class Solution {
- public:
- bool searchMatrix(vector<vector<int> > &matrix, int target) {
- int m = matrix.size();
- if(m<=0) return false;
- int n = matrix[0].size();
- for(int i=0; i<m; ++i){
- if(matrix[i][n-1] == target)
- return true;
- else if(matrix[i][n-1] < target)
- continue;
- else{
- for(int j=0; j<n; ++j)
- if(matrix[i][j] == target)
- return true;
- return false;
- }
- }
- return false;
- }
- };
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Search a 2D Matrix [25]的更多相关文章
- [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 搜索一个二维矩阵
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 ...
- 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 ...
- 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——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]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [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] Search a 2D Matrix 二分搜索
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- HibernateDaoSupport的getSession()与HibernateTemplate的区别
在 Spring+Hibernate的集成环境里,如果DAO直接使用HibernateDaoSupport的getSession()方法获取 session进行数据操作而没有显式地关闭该session ...
- POJ 2976 Dropping tests(二分答案)
[题目链接] http://poj.org/problem?id=2976 [题目大意] 给出每门成绩的总分和得分,去除k门成绩之后 使得剩余的成绩分数和除以总分得到的数字最大,要求精度在三位小数之 ...
- java 为pdf添加水印图片
首先需要引入两个Jar包分别是:iTextAsian.jar .itext-2.1.7.jar 可以去 http://download.csdn.net/detail/work201003/922 ...
- jquery中read与js中onload区别
在JavaScript中,onload函数是最经常使用的,几乎涉及到JavaScript的童鞋都少不了要接触它.这个函数的作用就是等待网页完全装载完了以后再去执行代码块内的语句,因为按照文档流的执行顺 ...
- 兼容所有浏览器的CSS3圆角
兼容所有浏览器的CSS3圆角 解决CSS3圆角兼容所有浏览器的方法.本文提到了一种很不错的实现跨浏览器圆角的解决方案,但是说的不够全面,前端观察最近将整理更多更全面的资源给大家,敬请期待. ...
- HBASE学习笔记--API
HBaseConfiguration HBaseConfiguration是每一个hbase client都会使用到的对象,它代表的是HBase配置信息.它有两种构造方式: public HBaseC ...
- NodeJS下载文件实例
var http = require('http');var express = require('express');var fs=require("fs"); var app ...
- android开发SDcard 响应的文件相关处理(一)
android开发相关文件类的处理工具类: package com.gzcivil.utils; import java.io.File; import java.util.ArrayList; im ...
- CDH(cdh5.7) 上集成 kafka
CDH 可以在线下载: 离线安装
- C++ 数据结构学习一(顺序表)
//SequentialList.h 顺序表模板类 #ifndef SEQUENTIAL_LIST_HXX#define SEQUENTIAL_LIST_HXX using std::cout; us ...