題目

在一個m x n的矩陣中,給予目標數,判斷目標數是否有在矩陣當中

解題方法

直接使用find()去尋找目標數是否有在矩陣當中。
有個缺點就是Runtime時間超級長,或許有更好的方法去解。
但我目前只想到這樣做最方便QQ

程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
bool ans = false;
for(int i=0;i<matrix.size();++i)
{
vector<int>::iterator it = find(matrix[i].begin(),matrix[i].end(),target);
if(it != matrix[i].end())
return true;
else if(i == matrix.size() - 1 && it == matrix[i].end())
return false;
}

return false;
}
};