LeetCode_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: [
[, , , ],
[, , , ],
[, , , ]
]
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
int n = matrix[].size();
if(target < matrix[][] || target >matrix[m-][n-])return false; int i,j; for(i = ;i< m-;i++)
if(target >=matrix[i+][])
continue;
else
break ; for(j = ; j< n; j++)
if(target == matrix[i][j])
return true;
else if(target <matrix[i][j])
return false;
}
};
感觉上面的方法虽然过了所有case,但还是可能有问题的,看了《剑指offer》后,才发现了一种更好的解法:
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
int n = matrix[].size(); int row = , column = n -;
while(row < m && column >= )
{ if(matrix[row][column] == target)
return true;
else if(matrix[row][column] > target)
column--;
else
row++;
}
return false;
}
};
LeetCode_Search a 2D Matrix的更多相关文章
- [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 ...
- HTML 学习笔记 CSS3 (2D Matrix)
Matrix 矩阵 那么什么是矩阵呢? 矩阵可以理解为方阵,只不过 平时方阵里面站着人 矩阵中是数值: CSS3中的矩阵: css3中的矩阵指的是一个方法,书写为matrix() 和 matrix3d ...
- Leetcode 74 and 240. Search a 2D matrix I and 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
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- poj1740 A New Stone Game
题意:对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆. 真是好♂题,代码不长就是好♂题. 首先考虑两堆相同的 ...
- DPI情况下处理
1. 字体不要跟着变大小,那就要使用setPixe,不要使用setPointSize 2. 图片可设置QPixmap::setDevicePixelRatio http://doc.qt.io/qt- ...
- C#进程间通讯技术-整理。
原文:C#进程间通讯技术-整理. 扩展阅读:http://www.cnblogs.com/joye-shen/archive/2012/06/16/2551864.html 一.进程间通讯的方式 1) ...
- javascript笔记5之流程控制语句
/* var box = 100; //单行语句 var age = 20; //另一条单行语句 { //用花括号包含的语句集合,叫做复合语句,单位一个 //一对花括号,表示一个复合语句,处理时候,可 ...
- 弹出框layer的使用封装
layer弹出框官方网址:http://layer.layui.com/ layer常用方法的封装:layerTool.jsp layer.config({ extend: 'extend/layer ...
- C#使用WebKitBrowser.dll填坑记
.Net 自带的 Webbrowser 有着太多的平台限制.对于用户体验之上的今天,这无疑是一个噩梦, 然后就开始找 .Net下的WebKitBrowser.dll (后面提供下载) 从开源网站下到程 ...
- C++ Primer 学习笔记_77_模板与泛型编程 --实例化
模板与泛型编程 --实例化 引言: 模板是一个蓝图,它本身不是类或函数.编译器使用模板产生指定的类或函数的特定版本号.产生模板的特定类型实例的过程称为实例化. 模板在使用时将进行实例化,类模板在引用实 ...
- [RxJS] Sharing Streams with Share
A stream will run with each new subscription added to it. This lesson shows the benefits of using sh ...
- Asp.net原理(第一篇)
Asp.net (第一篇) 当用户在浏览器输入一个URL地址后,浏览器会发送一个请求到服务器.这时候在服务器上第一个负责处理请求的是IIS.然后IIS再根据请求的URL扩展名将请求分发给不同的ISAP ...
- Android打开外部DB文件
DB文件要放在Assets文件夹下,封装一个工具类,如下: package com.XX.DB; import java.io.File; import java.io.FileOutputStrea ...