剑指offer阅读有感

/*二维数组中的查找
 * 在一个二维数组中,每一行都按照从左到右递增的顺序排序;
 * 每一列都按照从上到下递增的顺序排序。需要一个函数,输入
 * 这样一个二维数组中的一个整数 ,判断是否为此数组的元素;
 *
 *变量的定义:不再多说;
 *
 * 首先解决此问题前我需要明白二维数组需要手工定义的变量
 * 如行、列,还有输入的数以及一个标记二位数的地址变量
 * 以及定义一个一维数组存储指定元素的下标;
 *
 *
 * 编写测试程序,测试各种情况增强代码的鲁棒性(robust);
 * 这里测试程序以模块化编程的思想写成函数的形式;Test()
 */

#include <stdio.h>
#include <stdbool.h>

//bool Find (int * matrix, int *index, int rows, int columns, int number){
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; //数组元素的小标从0开始,所以减掉1;

                while(row < rows  && column >= 0){
                        if(number == matrix[row * columns + column]){   //右上角数字
                                Found = true;
                        //      index[0] = row;
                        //      index[1] = column;
                                break;  //我在使用break的时候犯了一个错误就是忘记了上面的while循环,
                                                //break只能用在循环语句中跳到外层循环;
                        }
                        else if(matrix[row*columns + column] > number){
                                --column;
                        }
                        else{
                                ++row;
                        }
                }
        }
        return Found;
}

void Test(char *Testname, int *matrix, int rows, int columns, int number, bool expected){
        if(Testname)
                printf("%s start: \n",Testname);
        bool result = Find(matrix, rows, columns, number);
        printf("result val is:%d\n",result);
        printf("expected val is:%d\n",expected);
        if(result )
                printf("Congratulations u find it !!!\n");
        else
                printf("There is some trouble ...!!\n");
}

/*The first test*/
//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数在数组中
void Test1 (void){
        int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test1", (int*)matrix, 4, 4, 7, true);
}

/*The second Test*/
//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数不在数组中
void Test2 (void){
        int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test2",(int *) matrix, 4, 4, 5, false);
}
/*3*/
//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数不在数组中

void Test3 (void){
        int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        Test("Test3", (int*)matrix, 4, 4, 1, true);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数是数组中最大的数字
void Test4()
{
    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
    Test("Test4", (int*)matrix, 4, 4, 15, true);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数比数组中最小的数字还小
void Test5()
{
    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
    Test("Test5", (int*)matrix, 4, 4, 0, false);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数比数组中最大的数字还大
void Test6()
{
    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
    Test("Test6", (int*)matrix, 4, 4, 16, false);
}

// 鲁棒性测试,输入空指针
void Test7()
{
    Test("Test7", NULL, 0, 0, 16, false);
}
/*这里为测试而用*/
#if 0
int main(int argc, char *argv[]){
        int index[2] = {0};     //数组初始化 ,用来保存符合条件元素的下标;
        int matrix[4][4] = {{1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,15}};
        printf("数组初始化成功、\n");
        bool ret = Find(matrix, index, 4, 4, 7);
        printf("函数调用成功成功、\n");

        printf("%d %d %d\n",ret, index[0], index[1]);
        return 0;
}
#endif
int main(int argc, char *argv[]){
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
        return 0;
}
一下为输出:
linux@ubuntu:~/workdir/interview/string_operation$ ./a.out
Test1 start:
result val is:1
expected val is:1
Congratulations u find it !!!
Test2 start:
result val is:0
expected val is:0
There is some trouble ...!!
Test3 start:
result val is:1
expected val is:1
Congratulations u find it !!!
Test4 start:
result val is:1
expected val is:1
Congratulations u find it !!!
Test5 start:
result val is:0
expected val is:0
There is some trouble ...!!
Test6 start:
result val is:0
expected val is:0
There is some trouble ...!!
Test7 start:
result val is:0
expected val is:0
There is some trouble ...!!

这里引用http://blog.csdn.net/zyw_ym_zone/article/details/9321749 

题目描述:在一个二维数组中,每一行元素从左到右递增,从上到下递增,现输入一个整数,判断数组中是否存在该整数,要求时间复杂度为O(n)

思路:可以数组分为三个部分,数组右上角,左边,下面

例如:数组如下

1  2  8    9

2  4  9   12

4  7  10 13

6  8  11 15

查找元需为7,数组右上角元素为9

因为7<9,则在9左边的三列中寻找7元素,将col减1

1  2  8

2  4  9

4  7  10

6  8  11

再取数组右上角元素8,7<8,则在8左边的两列中寻找7元素,将col减1

1  2

2  4

4  7

6  8

再取数组右上角元素2,7>2,则在2下边三行中寻找元素7,注意此时的3行,值

2  4 

4  7

6  8,前面已经将col至2啦

再取数组的右上角元素4,7>4,则在4下边2行中寻找

4  7

6  8

再取数组的右上角元素7,7==4,返回,查找成功


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值