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.

 
最简单的想法,转化成一维的,再用二分法:
 
 class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) { int row=matrix.size();
int col=matrix[].size();
vector<int> m(row*col); for(int i=;i<row;i++)
{
for(int j=;j<col;j++)
{
m[i*col+j]=matrix[i][j];
}
} int left=;
int right=m.size()-;
int mid;
while(left<=right)
{
mid=(left+right)/;
if(m[mid]>target)
right=mid-;
else if(m[mid]<target)
left=mid+;
else
return true;
}
return false; }
};
 
 
第二种思路,直接使用二分法
 
把一维数字转化为二维的坐标的方法:
第n个元素,在n/col行,n%col列
 
 class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) { int row=matrix.size();
int col=matrix[].size(); int left=;
int right=row*col-;
int mid;
int m;
while(left<=right)
{
mid=(left+right)/;
m=matrix[mid/col][mid%col];
if(m>target)
right=mid-;
else if(m<target)
left=mid+;
else
return true;
}
return false;
}
};

【leetcode】Search a 2D Matrix的更多相关文章

  1. 【leetcode】 Search a 2D Matrix (easy)

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

  2. 【Leetcode】【Medium】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. 【数组】Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  4. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

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

  5. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  6. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

  7. [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵

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

  8. 【题解】【数组】【查找】【Leetcode】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

随机推荐

  1. 关于Hash集合以及Java中的内存泄漏

    <学习笔记>关于Hash集合以及Java中的内存泄漏 标签: 学习笔记内存泄露hash 2015-10-11 21:26 58人阅读 评论(0) 收藏 举报  分类: 学习笔记(5)  版 ...

  2. 转载:Objective-C中的 instancetype 和 id 关键字

    Objective-C中的instancetype和id关键字 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/16994 ...

  3. HBase命令(一) -- 库操作

    打开数据库 bin/start-hbase.sh //打开HBase bin/hbase shell //以命令行的方式打开Hbase控制台 Rest接口开启 bin/hbase rest //普通的 ...

  4. Mac按键

    ⌘——Command ⌃ ——Control ⌥——Option (alt) ⇧——Shift ⇪——Caps Lock ⌫——Delete

  5. C#设计模式 - 观察者模式(使用委托)

    1.概念 观察者模式(有时又被称为发布-订阅Subscribe>模式.模型-视图View>模式.源-收听者Listener>模式或从属者模式)是软件设计模式的一种.在此种模式中,一个 ...

  6. 解决安装VS2013提示“已停止工作”问题

    新安装操作系统(win8.1),手动安装各种驱动,安装VS2013,报错,见下图: 原因:显卡驱动问题. 解决办法:卸载intel显卡驱动这碧池.(系统会自动给你适配合适的)

  7. [MongoDB]mapReduce

    摘要 上篇文章介绍了count,group,distinct几个简单的聚合操作,其中,group比较麻烦一点.本篇文章将学习mapReduce的相关内容. 相关文章 [MongoDB]入门操作 [Mo ...

  8. 代码中access 的使用

     C++代码:if(access(strZip.c_str(), 0) == 0){...}    此处为判断strZip中文件是否存在   .c_str() 是他自身字符串名称,该名称是一个压缩文件 ...

  9. 【8-18】JS学习01

    Source : http://www.w3school.com.cn/html 外部的 JavaScript 也可以把脚本保存到外部文件中.外部文件通常包含被多个网页使用的代码. 外部 JavaSc ...

  10. root用户自动登录

    编辑文件: /etc/gdm/custom.conf的内容: 1 # GDM configuration storage      2       3 [daemon]      4 #GtkModu ...