转载请注明作者和出处: http://blog.csdn.net/john_bh/
github地址:https://github.com/johnbhlm/SwordFingerOffer
完整代码,已经同步到github:https://github.com/johnbhlm/SwordFingerOffer,欢迎关注,欢迎star~
1. 题目
题目:二维数组中的查找
问题:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
举例:
例如下面的二维数组就是每行、每列都递增排序。如果在数组中找到数字7,则返回true,如果查找数字5,由于数组不含该数字,则返回false。
2. 解题思路
算法思想:我们知道每一行都是递增排序的,每一列也是从上到下递增排序的,所以左上角的数是最小的,而右下角的数是最大的
- 选取右上角的数(也可以从左下角开始,
但是不能从左上角或者右下角开始
); - 判断比较右上角的数和传入的数值的大小,如果等于该参数的值,查找结束;
- 如果大于参数,则可以把这一列剔除;
- 如果小于参数,就剔除该数值所在的行
也就是说如果要查找的数字不在数组右上角,则每次都在数组的查找范围中剔除一行或者一列,这样每一步都可以缩小查找范围,直到找到要查找的数字,或者查找范围为空。
查找过程如下:
3. python 实现
3.1 暴力解法
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
n=len(array)
isFound ='false'
for i in range(n):
if target in array[i]:
isFound ='true';
break
return isFound
while True:
try:
S=Solution()
# 字符串转为list
L=list(eval(raw_input()))
array=L[1]
target=L[0]
print(S.Find(target, array))
except:
break
3.2 分治解法
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
rows = len(array) - 1
cols= len(array[0]) - 1
i = rows
j = 0
while j<=cols and i>=0:
if target<array[i][j]:
i -= 1
elif target>array[i][j]:
j += 1
else:
return True
return False
4. C++ 实现
4.1 暴力解法
两层大循环,这个遍历,时间复杂度是 O ( n 2 ) O(n^2) O(n2),代码如下:
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int row = 0, col = 0, t = 0;
bool isFound = false;
for (int i = 0; i < array.size(); ++i) {
for (int j = 0; j < array[i].size(); ++j) {
if (false == isFound && target == array[i][j]) {
//已经找到后就没必要再找了
isFound = true;
return isFound;
}
}
}
return isFound;
}
};
4.2 分治解法
-
从右上角出发:
#include <iostream> #include <vector> using namespace std; class Solution { public: bool Find(int target, vector<vector<int> > array) { bool isFound = false; int rows = array.size(); int cols = array[0].size(); // 从右上角的元素找起,如果查找的元素比当前位置元素小, 就向左走; 如果查找的元素比当前位置元素大, 就向下走 for (int i = 0, j = cols - 1; (i >= 0 && i < rows) && (j >= 0 && j < cols);) { if (target == array[i][j]) { isFound = true; break; } else if(array[i][j] > target){//target在当前位置的左侧 j--; } else {//target在当前位置的下方 i++; } } return isFound; } }; void print(vector<vector<int> > array, int row, int cloumn) { for (int i = 0; i < row; i++) { for (int j = 0; j < cloumn; j++) { cout << array[i][j] << " "; } cout << endl; } cout << endl; } int main() { int a1[] = { 1, 2, 8, 9, }; int a2[] = { 2, 4, 9, 12, }; int a3[] = { 4, 7, 10, 13, }; int a4[] = { 6, 8, 11, 15, }; vector<vector<int>> array; array.push_back(vector<int>(a1, a1 + 4)); array.push_back(vector<int>(a2, a2 + 4)); array.push_back(vector<int>(a3, a3 + 4)); array.push_back(vector<int>(a4, a4 + 4)); int num; cout << "Enter the find number:" << endl; cin >> num; cout << "the matrix is:" << endl; print(array, 3, 4); Solution solu; if (solu.Find(num, array)) { cout << "find num:" << num << endl; } else { cout << "not find num:" << num << endl; } return 0; }
-
从左下角开始,代码如下:
#include <iostream> #include <vector> using namespace std; class Solution { public: bool Find(int target, vector<vector<int> > array) { bool isFound = false; int rows = array.size(); int cols = array[0].size(); //从左下角的元素找起,如果查找的元素比当前位置元素小, 就向上走; 如果查找的元素比当前位置元素大, 就向右走 for (int i = rows - 1, j = 0; (i >= 0 && i < rows) && (j >= 0 && j < cols);) { if (target == array[i][j]) { isFound = true; break; } else if (target < array[i][j]) { //target在当前位置的上方 i--; } else{ //target在当前位置的右侧 j++; } } return isFound; } }; void print(vector<vector<int> > array, int row, int cloumn) { for (int i = 0; i < row; i++) { for (int j = 0; j < cloumn; j++) { cout << array[i][j] << " "; } cout << endl; } cout << endl; } int main() { int a1[] = { 1, 2, 8, 9, }; int a2[] = { 2, 4, 9, 12, }; int a3[] = { 4, 7, 10, 13, }; int a4[] = { 6, 8, 11, 15, }; vector<vector<int>> array; array.push_back(vector<int>(a1, a1 + 4)); array.push_back(vector<int>(a2, a2 + 4)); array.push_back(vector<int>(a3, a3 + 4)); array.push_back(vector<int>(a4, a4 + 4)); int num; cout << "Enter the find number:" << endl; cin >> num; cout << "the matrix is:" << endl; print(array, 3, 4); Solution solu; if (solu.Find(num, array)) { cout << "find num:" << num << endl; } else { cout << "not find num:" << num << endl; } return 0; }
4.3 官方代码
官方代码:FindInPartiallySortedMatrix.cpp
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题4:二维数组中的查找
// 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按
// 照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个
// 整数,判断数组中是否含有该整数。
#include <cstdio>
bool Find(int* matrix, int rows, int columns, int number)
{
bool found = false;
if(matrix != nullptr && 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;
}
// ====================测试代码====================
void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected)
{
if(testName != nullptr)
printf("%s begins: ", testName);
bool result = Find(matrix, rows, columns, number);
if(result == expected)
printf("Passed.\n");
else
printf("Failed.\n");
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
void Test1()
{
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);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数不在数组中
void Test2()
{
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);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数是数组中最小的数字
void Test3()
{
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", nullptr, 0, 0, 16, false);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
return 0;
}