# 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 following properties:

Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,

Consider the following matrix:

[
[1,   4,  7, 11, 15],
[2,   5,  8, 12, 19],
[3,   6,  9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.

Given target = 20, return false.

1.从左下开始找，如果matrix[r,c]（r为行，c为列） < target，c++；否则r--。
2.直到r或c越界为止。

public class Solution {
public bool SearchMatrix(int[,] matrix, int target)
{
var rowLen = matrix.GetLength(0);
var colLen = matrix.GetLength(1);

var row = rowLen - 1;
var col = 0;

while(col <= colLen - 1 && row >= 0){
if(matrix[row,col] == target){
return true;
}
else if(matrix[row,col] < target){
col ++;
}
else{
row --;
}
}
return false;
}

}

• 本文已收录于以下专栏：

## LeetCode Search a 2D Matrix II

Description: Write an efficient algorithm that searches for a value in an m x n matrix. This ma...

## LeetCode OJ 240. Search a 2D Matrix II

LeetCode OJ 240. Search a 2D Matrix IIDescriptionWrite an efficient algorithm that searches for a va...

## Java 实现leetcode Search a 2D Matrix II

Java 实现leetcode Search a 2D Matrix II本片依旧是关于Leetcode 里面算法题目的详解，实现Search a 2D Matrix II问题算法 问题描述：在这个...

## Leetcode Search a 2D Matrix II

**题目：**Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has th...

## [leetcode] 240 Search a 2D Matrix II

• NK_test
• 2015年08月19日 12:15
• 962

## <LeetCode OJ> 74. / 240. Search a 2D Matrix （I / II）

240. Search a 2D Matrix II My Submissions Question Total Accepted: 28527 Total Submissions: 8556...

## LeetCode No.240 Search a 2D Matrix II

LeetCode No.240 Search a 2D Matrix II

举报原因： 您举报文章：LeetCode -- Search a 2D Matrix II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)