-
Notifications
You must be signed in to change notification settings - Fork 0
/
74-Search a 2D Matrix.cpp
88 lines (75 loc) · 2.15 KB
/
74-Search a 2D Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
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.
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
bool b = false;
if(matrix.size() == 0) return false;
int row_max = (int)matrix.size()-1;
int column_max = (int)matrix[0].size()-1;
if( target < matrix[0][0] || target > matrix[row_max][column_max]) return false;
for( int i = 0; i <= row_max; i++){
if(target >= matrix[i][0] && target <= matrix[i][column_max]){
b = find_target(matrix[i], target);
return b;
}
}
return b;
}
//ÔÚÒ»¸övectorÖÐÑ°ÕÒÒ»¸öÊý
bool find_target(vector<int> myArray, int target){
bool b = false;
vector<int>::iterator it;
for(it = myArray.begin(); it != myArray.end(); it++){
if(*it == target) {
b = true;
}
}
return b;
}
};
void print_vector (vector<vector<int>> matrix){
vector<vector<int>>::iterator it1;
for(it1 = matrix.begin(); it1 != matrix.end(); it1++){
vector<int> matrix_row = *it1;
vector<int>::iterator it2;
for(it2 = matrix_row.begin(); it2 != matrix_row.end(); it2++){
cout<<*it2<<" ";
}
cout<<endl;
}
}
const int M = 3;
const int N = 4;
int main()
{
int test[M][N] = { {1, 3, 5, 7},{10, 11, 16, 20},{23, 30, 34, 50}};
vector<vector<int>> matrix;
vector<int> temp;
for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++){
temp.push_back(test[i][j]);
}
matrix.push_back(temp);
temp.clear();
}
print_vector(matrix);
Solution s;
cout<<s.searchMatrix(matrix, 51)<<endl;
return 0;
}