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

//vs2012测试代码
#include<iostream>
#include<vector>

using namespace std;

#define M 1
#define N 2

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target)
	{
        int m=matrix[0].size();
		int n=matrix.size();
		int curX=0;
		if( m==0 || n==0)
			return false;
		if( target<matrix[0][0] || target>matrix[n-1][m-1])
			return false;
		for(int i=1; i<n; i++)
		{
			if(target>=matrix[i][0])
				curX++;
		}
		int i=0;
		int j=m-1;
		while(i<=j)
		{
			int temp = i+(j-i)/2;
			if(matrix[curX][temp] == target)
			{
				cout<<curX<<' '<<temp<<endl;
				return true;
			}
			else if(matrix[curX][temp] > target)
				j=temp-1;
			else if(matrix[curX][temp] < target)
				i=temp+1;
		}
		return false;
    }
};

int main()
{
	int a;
	int target;
	vector<int> one[N];
	vector<vector<int>> two;
	for(int j=0; j<N; j++)
	{
		for(int i=0; i<M; i++)
		{
			cin>>a;
			one[j].push_back(a);
		}
		two.push_back(one[j]);
	}
	cin>>target;
	Solution lin;
	cout<<lin.searchMatrix(two, target)<<endl;
	/*
	for(int j=0; j<N; j++)
	{
		for(int i=0; i<M; i++)
		{
			cout<<two[j][i];
		}
		cout<<endl;
	}
	*/
}
//方法一:自测Accepted
class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        int m=matrix[0].size();
		int n=matrix.size();
		int curX=0;
		if( m==0 || n==0)
			return false;
		if( target<matrix[0][0] || target>matrix[n-1][m-1])
			return false;
		for(int i=1; i<n; i++)
		{
			if(target>=matrix[i][0])
				curX++;
		}
		int i=0;
		int j=m-1;
		while(i<=j)
		{
			int temp = i+(j-i)/2;
			if(matrix[curX][temp] == target)
			{
				cout<<curX<<' '<<temp<<endl;
				return true;
			}
			else if(matrix[curX][temp] > target)
				j=temp-1;
			else if(matrix[curX][temp] < target)
				i=temp+1;
		}
		return false;
    }
};
//方法二:其他人版本
class Solution {
public:
    int searchRow(vector<vector<int> > &matrix, int target)
    {
        int rowNum = matrix.size();
        int colNum = matrix[0].size();
        int left = 0;
        int right = rowNum-1;
        while(left <= right)
        {
            int mid = left+(right-left)/2;
            int curNum = matrix[mid][colNum-1];
            if(curNum == target) return mid;
            else if(curNum > target) right = mid-1;
            else left = mid+1;
        }
        if(left >= 0 && left < rowNum && matrix[left][colNum-1] >= target) return left;
        else return false;
    }
    int searchColumn(vector<vector<int> > &matrix, int rowIdx, int target)
    {
        int rowNum = matrix.size();
        int colNum = matrix[0].size();
        int left = 0;
        int right = colNum-1;
        while(left <= right)
        {
            int mid = left+(right-left)/2;
            int curNum = matrix[rowIdx][mid];
            if(curNum == target) return mid;
            else if(curNum > target) right = mid-1;
            else left = mid+1;
        }
        return -1;
    }
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int n = matrix.size();
        if(n == 0) return false;
        int m = matrix[0].size();
        if(m == 0) return false;

        int rowIdx = searchRow(matrix, target);
        if(rowIdx == -1) return false;
        int columnIdx = searchColumn(matrix, rowIdx, target);
        if(columnIdx == -1) return false;
        return true;
    }
};
时间: 2024-10-08 03:30:49

leetcode_74_Search a 2D Matrix的相关文章

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description 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 ascend

【LeetCode】240. Search a 2D Matrix II

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 ascendin

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

[leedcode 240] 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.

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 fr

LeetCode: 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

240.Search in a 2D Matrix II

/* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用了BS,但是并不一定要有mid,这里就从最左下角到右上角 * 这个数大于同一列的,小于同一行的,只要跟target比较以后,就可以要么删除一列,要么删除一行--------找拐点 * 这个题目自己做的时候给出了一个nlog的方案,先是row从下往上走,在同一row里面再继续bs * 但是这个题目的好

[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