[编程题]二维数组中的查找

# 1721个回答

```#从右上角向下查找，然后向左查找
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
col_len = len(array[0])-1
row_len = len(array)-1
j = col_len
i = 0

while i <= row_len and j >= 0:
if target > array[i][j]:
i = i+1
elif target == array[i][j]:
return True
else:
j = j-1

return False

```

`/* 思路`
`* 矩阵是有序的`
查看全部

```两种思路

public class Solution {
public boolean Find(int [][] array,int target) {

for(int i=0;i<array.length;i++){
int low=0;
int high=array[i].length-1;
while(low<=high){
int mid=(low+high)/2;
if(target>array[i][mid])
low=mid+1;
else if(target<array[i][mid])
high=mid-1;
else
return true;
}
}
return false;

}
}

public class Solution {
public boolean Find(int [][] array,int target) {
int row=0;
int col=array[0].length-1;
while(row<=array.length-1&&col>=0){
if(target==array[row][col])
return true;
else if(target>array[row][col])
row++;
else
col--;
}
return false;

}
}```

Python版本
```# -*- 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```

C++版本
```class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
// array是二维数组，这里没做判空操作
int rows = array.size();
int cols = array[0].size();
int i=rows-1,j=0;//左下角元素坐标
while(i>=0 && j<cols){//使其不超出数组范围
if(target<array[i][j])
i--;//查找的元素较少，往上找
else if(target>array[i][j])
j++;//查找元素较大，往右找
else
return true;//找到
}
return false;
}
};```

Java版本
```public class Solution {
public boolean Find(int target, int [][] array) {
int rows = array.length;
int cols = array[0].length;
int i=rows-1,j=0;
while(i>=0 && j<cols){
if(target<array[i][j])
i--;
else if(target>array[i][j])
j++;
else
return true;
}
return false;
}
}```

public class Solution {
public boolean Find(int [][] array,int target) {
int len = array.length-1;
int i = 0;
while((len >= 0)&& (i < array[0].length)){
if(array[len][i] > target){
len--;
}else if(array[len][i] < target){
i++;
}else{
return true;
}
}
return false;
}
}

```class Solution {
public:
bool Find(vector<vector<int> > array,int target) {
int m,n,x,y;
m = array.size();//行数
n = array[0].size();//列数
x=m-1;y=0;//坐标定在左下角
while(x>=0 && y<=n-1){
if(target<array[x][y]){
x--;//遇小上移
}
else if(target>array[x][y]){
y++;//遇大右移
}
else {
return true;
}
}
return false;
}
};```

```public class Solution {
public boolean Find(int [][] array,int target) {
int m = array.length - 1;
int i = 0;
while(m >= 0 && i < array[0].length){
if(array[m][i] > target)
m--;
else if(array[m][i] < target)
i++;
else
return true;
}

return false;
}
}```
`java 版本正确的`

```其实也可以从右上开始寻找；
class Solution {
public:
bool Find(vector<vector<int> > array,int target) {
int row = array.size();
int col = array[0].size();
int i,j;
for (i=0,j=col-1;i<row && j>=0;)
{
if (array[i][j] == target)
{
return true;
}
if (array[i][j] > target)
{
j--;
continue;
}
if (array[i][j] < target)
{
i++;
}
}
return false;
}
};```

``````# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
if not array:
return
row=len(array)
col=len(array[0])
for i in range(row):
for j in range(col):
if array[i][j]==target:
return True
return False
``````

```public class Solution {
public boolean Find(int [][] array,int target) {
for(int[] i : array){
for(int j : i){
if(j==target)return true;
}
}
return false;
}
}

//方法简单,代码少
```

16,[[]]

false

```class Solution {
public:
bool Find(vector<vector<int> > array, int target) {
int Row = array.size();
int Col = array[0].size();

for (int i = 0, j = Col-1; i < Row && j >=0;)
{
if (target > array[i][j])
i++;
else if (target < array[i][j])
j--;
else if (target == array[i][j])
return true;
}
return false;
}
};

```

JS了解一下
function Find(target, array)
{
return array.some(arr=>arr.some(e=>e===target))
}

``````<?php

function Find(\$target, \$array)
{
// write code here
\$rows = count(\$array);//行
\$columns = count(\$array[0]);//列
\$rs = false;
//从右上角开始，
for (\$row = 0,\$column = \$columns - 1; \$row < \$rows && \$column >= 0;) {
if(\$array[\$row][\$column] == \$target){
\$rs = true;
break;
}
if (\$array[\$row][\$column] > \$target){
//大于目标数，剔除本列
\$column--;
}
if(\$array[\$row][\$column] < \$target) {
\$row++;
}
}
return \$rs;
}
``````

``````class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int i=0;
int j=array[0].size()-1;

while(i<array.size()&&j>=0){
if(array[i][j]==target)
return true;
if(array[i][j]<target)
i++;
else if(array[i][j]>target)
j--;
}
return false;
}
};
``````

target = 12；
int[][] array = {{1,2,8},{2,4,9,12},{4,7},{6,8,11,15}};

```public class Solution {

public boolean Find(int target, int [][] array) {
int r=array.length-1,c=0,maxCol=0;
for (int i=0;i<=r;i++)
if (array[i].length>maxCol)maxCol=array[i].length;//元素最多的一行，元素数量为maxCol
while (r>=0&&c<maxCol){
if (c >= array[r].length)r--; //若该行元素较少，不存在第c列元素，继续上移；
else if (array[r][c]<target)c++;
else if (array[r][c]>target)r--;
else if (array[r][c]==target)return true;
}
return false;
}
}```

```function Find(target, array) {
let lenX = array.length;
let lenY = array[0].length;
for (let i = lenX - 1, j = 0; i >= 0 && j < lenY;) {
if (target > array[i][j]) {
j++;
}else if(target < array[i][j]) {
i--;
}else {
return true
}
}
}```

```public class Solution {
public boolean Find(int target, int [][] array) {
int m = array.length;
int n = array[0].length;
int i = m-1,j = 0;
while(true) {
if(i == -1 || j == n)    //进行边界判断，原来是在两个elseif里判断的，但是空数组的话会有异常
return false;
if(target == array[i][j])
return true;
else if(target < array[i][j])    //这里用elseif，原来用的是if，移位后array[i][j]发生改变，下面的判断有可能为true造成结果错误。使用elseif也会减少比较次数
i--;
else if(target > array[i][j])
j++;
}
}
}```

```/*
算法思想：首先选取数组中右上角的数字，如果该数字等于要查找的数字，则查找过程结束；如果该数字大于要查找的数字，剔除这个数字所在的列；
如果该数字小于要查找的数字，剔除这个数字所在的行。这样每一步都可以缩小查找范围，直到找到要查找的数字，或者查找范围为空为止。*/
public class Solution {
public boolean Find(int target, int [][] array) {
//boolean found = false;
int i = 0,j = array[0].length - 1;
while(i <= array.length - 1  && j >= 0){
if(array[i][j] == target){
return true;
//break;
}else if(array[i][j] > target){
j--;
continue;
}
else{
i++;
continue;
}
}
return false;
}
}```

1721条回答 336331浏览

# 通过挑战的用户

• 2018-05-21 15:19:45
• 2018-05-21 15:15:00
• 2018-05-21 15:07:41
• 2018-05-21 15:07:39
• 2018-05-21 15:05:50

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题

QQ群：169195721