Leetcode No.74 Search a 2D Matrix
来源:互联网 发布:linux下etc没有inittab 编辑:程序博客网 时间:2024/05/23 05:09
Question:
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
.
C++ Codes:
class Solution {public: bool searchMatrix(vector<vector<int> > &matrix, int target) { int rows = matrix.size(); int cols = matrix[0].size(); int left = 0, right = cols * rows - 1; while (left != right){ int mid = (left + right - 1) >> 1; if (matrix[mid / cols][mid % cols] < target) <span style="white-space:pre"></span>{ <span style="white-space:pre"></span>left = mid + 1;} else { <span style="white-space:pre"></span>right = mid;} } return matrix[right / cols][right % cols] == target; }};
0 0
- Leetcode NO.74 Search a 2D Matrix
- Leetcode No.74 Search a 2D Matrix
- LeetCode--No.74--Search a 2D Matrix
- LeetCode No.240 Search a 2D Matrix II
- LeetCode No.240 Search a 2D Matrix II
- Leetcode 74 Search a 2D Matrix
- LeetCode 74: Search A 2D Matrix
- LeetCode 74 Search a 2D Matrix
- [leetcode 74] Search a 2D Matrix
- [LeetCode 74]Search a 2D Matrix
- leetcode || 74、Search a 2D Matrix
- leetcode-74 Search a 2D Matrix
- Leetcode[74]-Search a 2D Matrix
- 【Leetcode】Search a 2D Matrix #74
- LeetCode(74) Search a 2D Matrix
- Leetcode #74 Search a 2D Matrix
- Leetcode #74 Search a 2D Matrix
- leetcode 74: Search a 2D Matrix
- windows 8 wmware player centos 安装 伪分布式 hadoop
- HashMap的默认容量和加载因子
- 排序算法:快速排序
- Spring解析
- Android开发中图标像素大小规范
- Leetcode No.74 Search a 2D Matrix
- hdu3308 LCIS(区间求和)
- POJ 2312 Battle City【Bfs+优先队列】
- nginx变量使用方法详解(2)
- 【Struts2】:自定义Interceptor
- 面向接口编程以及面向接口编程的好处
- 膨胀和腐蚀
- HashMap的内部实现机制,Hash是怎样实现的,什么时候ReHash
- 蓝桥杯 历届试题 城市建设 最小生成树