剑指offer 算法题(搜索二维矩阵)

剑指offer 第二题

去力扣里测试算法

思路一:

直接暴力遍历二维数组。

cpp 复制代码
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;
    }
};

本地代码:

cpp 复制代码
#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 = 5
    for (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;
}

思路二:

相关推荐
朴shu3 分钟前
Delta数据结构:深入剖析高效数据同步的奥秘
javascript·算法·架构
mit6.82427 分钟前
博弈dp|凸包|math分类
算法
Shinom1ya_1 小时前
算法 day 41
数据结构·算法·leetcode
hetao17338371 小时前
2025-10-30 ZYZOJ Star(斯达)模拟赛 hetao1733837的record
c++·算法
无敌最俊朗@1 小时前
C++ 值类别与移动语义详解(精简版)
java·数据结构·算法
lingran__2 小时前
算法沉淀第十一天(序列异或)
c++·算法
一匹电信狗2 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
寂静山林3 小时前
UVa 11853 Paintball
算法
西西弗Sisyphus3 小时前
线性代数 - 矩阵求逆
线性代数·矩阵·矩阵求逆·逆矩阵·单位矩阵
Theodore_10223 小时前
深度学习(10)模型评估、训练与选择
人工智能·深度学习·算法·机器学习·计算机视觉