zoukankan      html  css  js  c++  java
  • 二维数组中的查找-剑指 offerP38

    题目:

      在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    解题思路:《剑指 offer》P38

    代码:

    //  main.cpp
    //  二维数组中的查找
    //
    //  Created by Mr.G on 2019/1/14.
    //  Copyright © 2019 Mr.G. All rights reserved.
    //
    
    #include <iostream>
    
    bool Find(int* matrix, int rows, int columns, int number)
    {
        bool found = false;
        
        if(matrix != NULL && rows > 0 && columns > 0)
        {
            int row = 0;
            int column = columns - 1;
            while(row < rows && column >=0)
            {
                if(matrix[row * columns + column] == number)
                {
                    found = true;
                    break;
                }
                else if(matrix[row * columns + column] > number)
                    -- column;
                else
                    ++ row;
            }
        }
        
        return found;
    }
    
    int main(int argc, const char * argv[]) {
        
        int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        bool result = Find((int*)matrix, 4, 4, 7);
        if(result == true){
            printf("success
    ");
        }else{
            printf("falid
    ");
        }
        return 0;
    }
    
    2019/1/14

                                                                                                                                                                                           

  • 相关阅读:
    P3704 [SDOI2017]数字表格
    CF 700 E. Cool Slogans
    杜教筛学习笔记
    [BOI2004]Sequence 数字序列(左偏树)
    [WC2007]剪刀石头布(最大流)
    [NOI2009]变换序列(二分图匹配)
    文理分科(最小割)
    上帝与集合的正确用法(欧拉定理)
    [HAOI2008]圆上的整点(数论)
    主席树学习笔记
  • 原文地址:https://www.cnblogs.com/xiaovw/p/10268662.html
Copyright © 2011-2022 走看看