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 搜索一个二维矩阵的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

  9. 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 ...

随机推荐

  1. Jmeter调试工具---Debug Sampler

    一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...

  2. JavaScript Patterns 3.7 Primitive Wrappers

    Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 1 ...

  3. HDU 4049 Tourism Planning(动态规划)

    Tourism Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. poj 2195 KM算法

    题目链接:http://poj.org/problem?id=2195 KM算法模板~ 代码如下: #include "stdio.h" #include "string ...

  5. Uva-11374-Airport Express

    A - Airport Express Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Appoi ...

  6. 绘制复数图形和双y轴图形

    clearclct=0:0.1:2*pi;x=sin(t);y=cos(t);z=x+i*y;subplot(1,3,1)plot(t,z,'r') %注:这种方式下,不论参数t,z哪个是复数,都将忽 ...

  7. 关于字符编码:为什么在ASCII字符集中小写字母比大写字母大32

    今天看字符编码,在ASCII一节,字母'A'用ASCII编码是十进制的65,自然想到了'a'应该和'A'差个26吧,却发现'a'是十进制的97,差了32.突然很想知道为什么. 查了ASCII码表,发现 ...

  8. Hadoop question list

    1.我们在开发普通的web app的时候,总会用到orm框架,如hibernates ,ibates等,在hadoop中我们一直使用writable对象,当然如果我们想实现自己的对象类,需要继承这个w ...

  9. [转]NPOI TestFunctionRegistry.cs

    本文转自:https://github.com/tonyqus/npoi/blob/master/testcases/main/SS/Formula/TestFunctionRegistry.cs   ...

  10. uva 725 division(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVMAAAOHCAIAAAClwESxAAAgAElEQVR4nOydybGturJFcQEPfgQu4A