[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 ...
随机推荐
- Xcode 文件删除拷贝 出现的问题
当删除一个组的时候,不管是下面的两个选择,是彻底删除还是不彻底: 然后又要往工程里拷贝进去 同名 文件组,最好是选择Creat groups (因为创建groups就不会有import的时候,还需 ...
- Linux文件I/O学习
Linux内核的VFS子系统: 文件描述符 对于内核而言,所有打开的文件都通过文件描述符引用.文件描述符是一个非负整数.当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符.当读 ...
- 推送(PUSH)
一.本地推送 1.建立本地推送 UILocalNotification *notification = [[UILocalNotification alloc] init]; //推送的内容 noti ...
- Head First HTML CSS XHTML笔记
最近在看点前端的东西,看到了这本入门级的好书 <head></head>中的title和style <q></q> inline元素 在<p> ...
- python yield
http://www.jb51.net/article/15717.htm 这里还不错 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist(alist) ...
- Java中的 WeakReference 和 SoftReference
我们知道Java语言中没有指针,取而代之的是引用reference.Java中的引用又可以分为四种:强引用,弱引用(WeakReference),软引用(SoftReference),虚引用(Phan ...
- jquery实践案例--验证电子邮箱
<input type="email" name="email" id="email" value="" onpa ...
- Linux objcopy命令
一.简介 [功能] 将目标文件的一部分或者全部内容拷贝到另外一个目标文件中,或者实现目标文件的格式转换. [描述] objcopy工具使用BFD库读写目标文件,它可以将一个目标文件的内容拷贝到另外一个 ...
- java Memorymapfile demo
String lineseperator = java.security.AccessController .doPrivileged(new sun.security.action.GetPrope ...
- 基于对话框的MFC应用程序基本结构
新建一个基于对话框的MFC应用程序,假设命名为 Test:则该应用程序在刚创建的时候,有4个非常重要的文件和3个类: 4个非常重要的文件 1.Test.h 2.Test.cpp (应用程序类头文件) ...