博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Search a 2D Matrix】cpp
阅读量:6705 次
发布时间:2019-06-25

本文共 4070 字,大约阅读时间需要 13 分钟。

题目:

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
>& matrix, int target) { if (matrix.size()==0) return false; const int ROW = matrix.size(); const int COL = matrix[0].size(); // search the target row int begin = 0; int end = ROW-1; while ( begin<=end ) { int mid = (begin+end)/2; int lower = matrix[mid][0]; int upper = matrix[mid][COL-1]; if ( target>=lower && target<=upper ) { return Solution::binarySearch(matrix[mid], target); } if ( target
upper ) { begin = mid+1; continue; } } return false; } static bool binarySearch(vector
& row, int target) { int begin = 0; int end = row.size()-1; while ( begin<=end ) { int mid = (begin+end)/2; if ( row[mid]==target ) return true; if ( row[mid]>target ) { end = mid-1; } else { begin = mid+1; } } return false; }};

tips:

1. 首先二分查找可能所在的行

2. 确定某一行之后,再二分查找所在的列

完毕。

======================================

另一种思路:把大的二维矩阵当成一个一维数组看,只需要执行一次二分查找就OK了。

class Solution {public:    bool searchMatrix(vector
>& matrix, int target) { if (matrix.size()==0) return false; const int ROW = matrix.size(); const int COL = matrix[0].size(); int begin = 0; int end = ROW*COL-1; while ( begin<=end ){ int mid = (begin+end)/2; int val = matrix[mid/COL][mid%COL]; if ( val==target ) return true; if ( val>target ){ end = mid-1; } else{ begin = mid+1; } } return false; }};

tips:

注意这条语句“int val = matrix[mid/COL][mid%COL]”。

一开始写成了"int val = matrix[mid/ROW][mid%COL]" 掉入了这个思维陷阱,因为每行有多少列应该是基础长度单元,所以不论是取商还是余数,分母上都应该是COL。

============================================

第二次过这道题,沿用一般的思路,先找可能在哪一行;再去行里面找。

class Solution {public:    bool searchMatrix(vector
>& matrix, int target) { // search for row int row = -1; int begin = 0; int end = matrix.size()-1; while ( begin<=end ) { int mid = (begin+end)/2; if ( matrix[mid][0]<=target && matrix[mid][matrix[mid].size()-1]>=target ) { row = mid; break; } else if ( matrix[mid][0]>target ) { end = mid-1; } else { begin = mid+1; } } if ( row==-1 ) return false; // search in the row begin = 0; end = matrix[row].size()-1; while ( begin<=end ) { int mid = (begin+end)/2; if ( matrix[row][mid]==target ) return true; if ( matrix[row][mid]>target ) { end = mid-1; } else { begin = mid+1; } } return false; }};

 

转载于:https://www.cnblogs.com/xbf9xbf/p/4514943.html

你可能感兴趣的文章
Classloader和线程
查看>>
浅谈Attribute [C# | Attribute | DefaultValueAttribute]
查看>>
citus对join的支持
查看>>
【Cocos2d-X】iOS6 中 libcurl.a及iOS6中无法横屏的解决方法
查看>>
.NET开发常用知识点总结汇总
查看>>
源码配置bind主从时的注意事项
查看>>
英语每日听写练习 Day 6
查看>>
数组逆序重放(链表头插法练习)
查看>>
windows server 2008 安装实录
查看>>
安装卸载图形界面
查看>>
修改EXCHANGE默认的收发邮件大小是10M
查看>>
软raid的详细配置讲解 raid 0
查看>>
large-scale analysis of malware downloaders
查看>>
一道中级运维的shell面试题
查看>>
erlang: Programming Rules and Conventions。
查看>>
分布式应急响应
查看>>
iso定制封装
查看>>
精通MVC3摘译(8)-处理输出(2)
查看>>
字符串翻转之实现二
查看>>
Windows server 2008 Hyper-v下,玩转office communicator Server 2007 Enterprise
查看>>