版权所有,欢迎转载,转载请注明出处,谢谢
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