标签:
1 bool Find(const int *matrix, int rows, int columns, int number) 2 { 3 int key; 4 int indexRow; 5 int indexCol; 6 7 /*合法性检查*/ 8 if((NULL == matrix)||(rows <= 0)||(columns <=0)) 9 { 10 return false; 11 } 12 13 /*提升性能,直接和最大最小值比较*/ 14 if((number < matrix[0])||(number > matrix[rows*columns-1])) 15 { 16 return false; 17 } 18 19 indexRow = 0; 20 indexCol = columns - 1; 21 22 /*核心代码*/ 23 while((indexRow < rows)&&(indexCol >= 0)) 24 { 25 key = matrix[indexRow*columns + indexCol]; 26 if(key > number) 27 { 28 indexCol--; 29 } 30 else if(key < number) 31 { 32 indexRow++; 33 } 34 else 35 { 36 return true; 37 } 38 } 39 40 return false; 41 }
标签:
原文地址:http://www.cnblogs.com/cauchy007/p/4558631.html