leetcode_74题——Search a 2D Matrix(数组查找)

Search a 2D Matrix

Total Accepted: 40009 Total Submissions: 127082My Submissions

Question Solution

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:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

Hide Tags

Array Binary Search

Have you met this question in a real interview?

Yes

No

Discuss

#include<iostream>
#include <vector>
using namespace std;

/*这道题是在一个排好序的数组中查找一个指定的数
  从上到下递增,从左到右递增,所以从右往左,每次与最上面的那个数作比较*/
bool searchMatrix(vector<vector<int>>& matrix, int target) {
	int x=matrix.size();
	int y=matrix[0].size();

	for(int j=y-1;j>=0;j--)
	{
		if(target==matrix[0][j])
			return true;
		if(target>matrix[0][j])
		{
			for(int i=1;i<=x-1;i++)
				if(target==matrix[i][j])
					return true;
		}
	}
	return false;
}

int main()
{
	vector<vector<int> > vec0;
	vector<int> vec;
	vec.push_back(1);
	vec0.push_back(vec);
	vec.clear();
	vec.push_back(3);
	vec0.push_back(vec);
	cout<<searchMatrix(vec0,3)<<endl;

}

  

时间: 2024-10-24 01:18:22

leetcode_74题——Search a 2D Matrix(数组查找)的相关文章

[LeetCode]28. 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 ro

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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 ro

Leetcode 二分查找 Search a 2D Matrix

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search a 2D Matrix Total Accepted: 10871 Total Submissions: 35629 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in e

【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 ro

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

[LeetCode] Search a 2D Matrix [25]

题目 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

Search a 2D Matrix leetcode java

题目: 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 previou

[LintCode] Search a 2D Matrix

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

[leetcode]Search a 2D Matrix @ Python

原题地址:https://oj.leetcode.com/problems/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 o