您的位置:首页 > 职场人生

剑指Offer面试题3[二维数组中的查找]

2017-07-18 13:39 232 查看

1. 题目描述:

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

2. 相关知识:

数组在内存中是占据连续的空间并按顺序存储,C/C++语言中多维数组按行优先存储

例如,矩阵A[m]
在内存中的存储顺序为:

A[0][0]、A[0][1]…A[0]
、A[1][1]、A[1][1]…A[1]
…A[m]
、A[m][1]…A[m]

3. 解答思路:

从右上角开始,或者从左下角开始

4. 代码实现:

Python语言

使用列表(list)

# coding=utf-8
class Solution:
# 剑指Offer,第3道题
# array 二维列表
def Find(self, target, array):
# 从右上角开始
rows = len(array)
columns = len(array[0])
if rows > 0 and columns > 0:
r = 0
c = columns - 1
while(c >= 0 and r < rows):
if array[r][c] == target:
return True
if array[r][c] > target:
c = c - 1
else:
r = r + 1
return False

def Find2(self, target, array):
# 从左下角开始
rows = len(array)
columns = len(array[0])
if rows > 0 and columns > 0:
r = rows - 1
c = 0
while(c < columns and r >= 0):
if array[r][c] == target:
return True
if array[r][c] > target:
r = r - 1
else:
c = c + 1
return False

if __name__ == '__main__':
s = Solution()
print s.Find(7, [[1, 2, 8, 9], [4, 7, 10, 13]])
print s.Find2(7, [[1, 2, 8, 9], [4, 7, 10, 13]])


C语言

使用数组

#include "stdafx.h"

//从左下角开始
bool Find(int *matrix, int rows, int columns, int target)
{
if(matrix != NULL && rows > 0 && columns > 0)
{
int r = rows - 1;
int c = 0;
while(r>=0 && c<columns)
{
printf("%d ",matrix[r*columns+c]);
if (matrix[r*columns+c] == target)
{
return true;
}
if (matrix[r*columns+c] < target)
c = c + 1;
else
r = r - 1;
}
}
return false;
}

//从右上角开始
bool Find2(int *matrix, int rows, int columns, int target)
{
if(matrix != NULL && rows > 0 && columns > 0)
{
int r = 0;
int c = columns - 1;
while(r<rows && c>=0)
{
if (matrix[r*columns+c] == target)
{
return true;
}
if (matrix[r*columns+c] < target)
r = r + 1;
else
c = c - 1;
}
}
return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
int matrix[2][4] = {{1, 2, 8, 9}, {4, 7, 10, 13}};
int rows = 2;
int columns = 4;
int target = 7;
printf("result:%d\n",Find((int*)matrix, rows, columns, target));
printf("result:%d\n",Find2((int*)matrix, rows, columns, target));
return 0;
}


C++语言

使用vector

// ex03_cpp.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<vector>
#include<iostream>
using namespace std;

//从左下角开始
bool Find(int target, vector<vector<int> > matrix)
{
int rows = matrix.size();
int columns = matrix[0].size();
if(rows > 0 && columns > 0)
{
int r = rows - 1;
int c = 0;
while(r>=0 && c<columns)
{
if(matrix[r][c] == target)
{
return true;
}
if(matrix[r][c] < target)
c = c + 1;
else
r = r - 1;
}
}
return false;
}

//从右上角开始
bool Find2(int target, vector<vector<int> > matrix)
{
int rows = matrix.size();
int columns = matrix[0].size();
if(rows > 0 && columns > 0)
{
int r = 0;
int c = columns - 1;
while(r>=0 && c<columns)
{
if(matrix[r][c] == target)
{
return true;
}
if(matrix[r][c] < target)
r = r + 1;
else
c = c - 1;
}
}
return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
int const rows = 2;
int const columns = 4;
int test_data[rows][columns] = {{1, 2, 8, 9}, {4, 7, 10, 13}};
vector<vector<int> > matrix(rows,vector<int>(columns));
for(int i = 0; i < rows; ++i){
for (int j = 0; j < columns; ++j){
matrix[i][j]=test_data[i][j];
}
}
int const target = 7;
cout << Find(target, matrix) << endl;
cout << Find(target, matrix) << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面试题