zoukankan      html  css  js  c++  java
  • // 二维数组查找

    // 二维数组查找



    #include "stdafx.h"
    using namespace std;
    #include <string>
    #include <vector>

    class Solution {
    public:
        bool Find(int target, vector<vector<int> > array) {
            int rows = array.size();
            int cols = array[0].size();
            if (!array.empty() && rows > 0 && cols > 0)
            {
                int row = 0;
                int col = cols - 1;
                while (row < rows && col >= 0)
                {
                    if (array[row][col] > target)
                    {
                        col = col - 1;
                    }
                    else if (array[row][col] < target)
                    {
                        row = row + 1;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    };

    int main()
    {
        int array[4][4] = {
                            { 1, 2, 8, 9 },
                            { 2, 4, 9, 12 },
                            { 4, 7, 10, 13 },
                            {6,8,11,15} ,
                        };
        vector<int> aa = { 1, 2, 8, 9 };
        vector<int> bb = { 2, 4, 9, 12 };
        vector<int> cc = { 4, 7, 10, 13 };
        vector<int>dd = { 6, 8, 11, 15 };

        vector<vector<int>> kk = { aa, bb, cc, dd };
        Solution sou;
        sou.Find(7, kk);
        return 1;
    }

    天天向上
  • 相关阅读:
    Python-数据结构
    优化算法-BFGS
    Sparse AutoEncoder简介
    Sparse Filtering简介
    基于受限玻尔兹曼机(RBM)的协同过滤
    Grep学习笔记
    【深度学习篇】--Windows 64下tensorflow-gpu安装到应用
    【自然语言处理篇】--聊天机器人从初始到应用
    【深度学习】--DCGAN从入门到实例应用
    【深度学习】--GAN从入门到初始
  • 原文地址:https://www.cnblogs.com/hg07/p/12731408.html
Copyright © 2011-2022 走看看