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 ...
随机推荐
- 自制单片机之十一……模数转换IC ADC0809
我们重在实际制做,太罗嗦的内容我就不说了,只讲些跟制做有关的最精炼的知识. ADC0809是可以将我们要测量的模拟电压信号量转换为数字量从而可以进行存储或显示的一种转换IC.下面是它的管脚图和逻辑图: ...
- Linux磁盘及文件系统管理 2---- 使用fdisk进行磁盘管理
1 FDISK分区工具 1 fsidk是来自IBM的分区工具,支持绝大多数的操作系统,几乎所有的Linux都装有fdisk 2 fdisk是一个支持MBR的分区工具,如果要使用GPT的话我们无法使用f ...
- 用于防SQL注入的几个函数
不要相信用户的在登陆中输入的内容,需要对用户的输入进行处理 SQL注入: ' or 1=1 # 防止SQL注入的几个函数: addslashes($string):用反斜线引用字符串中的特殊字符' & ...
- 漫话Unity3D(一)
前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...
- JMeter分布式性能测试
利用JMeter进行负载测试的时候,使用单台机器模拟测试超过1000个行程的并发就有些力不从心,在执行的过程中,JMeter自身会自动关闭,要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...
- java -jdk配置1(环境变量配置)
此文转载自:http://www.cnblogs.com/nicholas_f/articles/1494073.html 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1 ...
- FragmentPagerAdapter与FragmentStatePagerAdapter差异
平常使用的FragmentPagerAdapter和FragmentStatePagerAdapter来自android.support.v4.app包用来构建ViewPager. FragmentP ...
- git 学习笔记一
1.git的 介绍 分布式和 集中式 集中式版本控制系统最大的毛病就是必须联网才能工作,如果在局域网内还好,带宽够大,速度够快,可如果在互联网上,遇到网速慢的话,可能提交一个10M的文件就需要5分钟, ...
- C语言随记-1
涉及指针.数组.函数指针 几种声明形式 int *a[5]; // a是一个有5个元素的数组,每个元素是整数类型指针(int *) int *a[] = {0x100, 0x104, 0x108, 0 ...
- CentOS6.6修改主机名和网络信息
1.修改主机名称 [root@centos ~]# vim /etc/sysconfig/network #打开文件,修改以下内容并保存 NETWORKING=yes #使用网络HOSTNAME=ce ...