[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 method to find an element.
LeetCode上的原题,请参见我之前的博客Search a 2D Matrix 搜索一个二维矩阵和Search a 2D Matrix II 搜索一个二维矩阵之二。
class Solution {
public:
bool findElement(vector<vector<int> > &matrix, int elem) {
if (matrix.empty() || matrix[].empty()) return false;
int row = , col = matrix[].size() - ;
while (row < matrix.size() && col >= ) {
if (matrix[row][col] == elem) return true;
else if (matrix[row][col] < elem) ++row;
else --col;
}
return false;
}
};
[CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵的更多相关文章
- [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 ...
- [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 ...
- [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] 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 ...
- [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组
13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...
- [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 ...
- [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索
11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
- 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 ...
随机推荐
- virtualbox 中ubantu虚拟机范文win7文件夹
宿主机操作系统: windows7_x64 旗舰版 Oracle VM virtualBox版本:5.0.12 虚拟机操作系统: Ubantu15.12_x64 Desktop版本 让虚拟机可以访 ...
- HTML页面定时跳转方法
1)html的实现 <head> <meta http-equiv="refresh" content="5;url=hello.html"& ...
- 邮件群发工具(C#版)
引言 经常会参与组织一些社区活动,涉及到和不同的人进行交流,微信当然是必须的,同样邮件也是一种不可或缺的方式. 一般群发的邮件不是很友好,如果是一对一的,收到邮件的人是不是会比较重视,而且还有他的名字 ...
- 大家一起和snailren学java-(四)初始化与清理
初始化和清理,是一个生命周期的起始.在java中,对象的初始化和对象的回收是怎样的呢? 在开发c++的时候,有构造器的概念.即对象的创建,首先默认调用构造器进行初始化.在java中也有“构造器”.ja ...
- SQLServer中char与varchar的区别
今天写一个项目的用户登录部分 刚开始做,所以是数据库和程序一起写 一开始没注意 在定义表T_Person时吧PerID和PerPwd的类型设定都是char(20) 并且写入了几个数据,诸如 id:01 ...
- JDK中的并发bug?
最近研究Java并发,无意中在JDK8的System.console()方法的源码中翻到了下面的一段代码: private static volatile Console cons = null; / ...
- 如何在CALayer设置滤镜
网上有很多关于CALayer中设置filtes属性的相关资料比如如何设置一个带滤镜的layer,代码如下: NSImage* image = [NSImage imageNamed:@"IM ...
- nyoj 38 布线问题
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=38 最小生成树水题~ 代码: #include "stdio.h" / ...
- sql server 之函数小技巧 && 整数类型为空是用空字符串替代实现
1.判空函数 说明:使用指定的替换值替换 NULL. 语法:ISNULL ( check_expression , replacement_value ) 参数: check_expression:将 ...
- Dos
一.简介 https://zh.wikipedia.org/wiki/DOS 二.系统下载 http://www.cn-dos.net/newdos/doswarea.htm 三.实用命令 1)查看系 ...