• 二维数组中的查找-剑指 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

                                                                                                                                                                                           

  • 相关阅读:
    [转载]RTSP in Stagefright
    FFMPEG for WMA Build Script
    Merge AACExtractor from ICS to Froyo
    查看部署在Heroku上的项目信息
    About AudioSystem Mute
    C++标准转换运算符reinterpret_cast
    纯CSS动态效果的画廊
    基于正则表达式匹配的C++语法高亮度显示
    C++标准转换运算符static_cast
    C++标准转换运算符const_cast
  • 原文地址:https://www.cnblogs.com/xiaovw/p/10268662.html
Copyright © 2020-2023  润新知