剑指offer 第二题
去力扣里测试算法
思路一:
直接暴力遍历二维数组。
class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {for (unsigned int i{ 0 }; i < matrix.size(); i++){for (unsigned int j{ 0 }; j < matrix[i].size(); j++){if (matrix[i][j] == target) {return true;}}} return false;}
};
本地代码:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main()
{vector<vector<int> > vect{ { 1, 4, 7, 11, 15 },{2, 5, 8, 12, 19},{3, 6, 9, 16, 22},{10, 13, 14, 17, 24},{18, 21, 23, 26, 30}};int target = 30;//cout << vec[0].size() << endl;//matrix = [[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]], target = 5for (unsigned int i{ 0 }; i < vect.size(); i++){for (unsigned int j{ 0 }; j < vect[i].size(); j++){cout << vect[i][j] << " ";if (vect[i][j] == target) {cout << " " << endl;cout << "找到了" << " ";}}}return 0;
}