• 练习题-二维数组中的查找


    《剑指offer》中的一个题目:

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

    例如,下面的二位数组就是每行、每列都递增排序。如果在这个数组中查找数字7,则返回true;如果查找数字5,由于数组不含该数字,则返回false。

        int matrix[] = {
            1, 2,  8,  9,
            2, 4,  9, 12,
            4, 7, 10, 13,
            6, 8, 11, 15
        };

    代码:

     1 #include <stdio.h>
     2 /*
     3  * 在一个二维数组中,每一行都按照从左到右递增的顺序排序,
     4  * 每一列都按照从上到下递增的顺序排序。请完成一个函数,输入
     5  * 这样一个二维数组和一个整数,判断数组中是否含有该整数。
     6  * rows   : 行数
     7  * comumns: 列数
     8  */
     9 int find_matrix_0(int *matrix, int rows, int columns, int number)
    10 {
    11     int found = -1;
    12 
    13     // 从右上角开始查找
    14     int row = 0;
    15     int column = columns - 1;
    16 
    17     while(row < rows && column >= 0)
    18     {
    19         if(matrix[row * columns + column] == number)
    20         {
    21             found = row * columns + column;
    22             break;
    23         }
    24         else if(matrix[row * columns + column] > number)
    25         {
    26             // 如果当前值比查找值大,则当前的一整列都比查找值大,从而跳过当前列 
    27             column--;
    28         }
    29         else
    30         {
    31             // 如果当前值比查找值小,则当前的一整行都比查找值小,从而跳过当前行
    32             row++;
    33         }
    34     }
    35 
    36     return found;
    37 }
    38 
    39 int find_matrix_1(int *matrix, int rows, int columns, int number)
    40 {
    41     int found = -1;
    42 
    43     // 从左下角开始查找
    44     int row    = rows - 1;
    45     int column = 0;
    46     while(row >= 0 && column < columns)
    47     {
    48         if(matrix[row * columns + column] == number)
    49         {
    50             found = row * columns + column;
    51             break;
    52         }
    53         else if(matrix[row * columns + column] > number)
    54         {
    55             // 如果当前值比查找值大,则当前一整行都比查找值大,从而跳过当前行
    56             row--; 
    57         }
    58         else
    59         {
    60             // 如果当前值比查找值小,则当前一整列都比查找值小,从而跳过当前列
    61             column++; 
    62         }
    63     }
    64 
    65     return found;
    66 }
    67 
    68 int main(void)
    69 {
    70     int matrix[] = {
    71         1, 2,  8,  9,
    72         2, 4,  9, 12,
    73         4, 7, 10, 13,
    74         6, 8, 11, 15
    75     };
    76     int found;
    77 
    78     found = find_matrix_0(matrix, 4, 4, 7);
    79     printf("find_matrix_0 : %d
    ", found);
    80 
    81     found = find_matrix_1(matrix, 4, 4, 7);
    82     printf("find_matrix_1 : %d
    ", found);
    83 
    84     return 0;
    85 }
  • 相关阅读:
    快速上手Unity原生Json库
    Unity3D安卓出包报错
    Git快速入门
    [Modern OpenGL系列(四)]在OpenGL中使用Shader
    [Modern OpenGL系列(三)]用OpenGL绘制一个三角形
    [Modern OpenGL系列(二)]创建OpenGL窗口
    [Modern OpenGL系列(一)]十步搞定OpenGL开发环境
    [Unity游戏开发]向量在游戏开发中的应用(三)
    [Unity游戏开发]向量在游戏开发中的应用(二)
    【leetcode】-两数之和
  • 原文地址:https://www.cnblogs.com/utank/p/4773663.html
Copyright © 2020-2023  润新知