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

# 2098个回答

public class Solution {
public boolean Find(int target, int[][] array) {
int rows = array.length;
int cols = array[0].length;
boolean temp=false;
for (int i = 0; i < rows && temp==false; i++) {
for (int j = 0; j < cols; j++) {
if (array[i][j] == target) {
temp=true;
break;
}
}
}
return temp;
}
}

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

```两种思路

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;
}
}```

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

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 版本正确的`

```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
}
}
}```

```其实也可以从右上开始寻找；
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;
}
}

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

``````<?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;
}
``````

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;
}
};

```

```public class Solution {
public boolean Find(int target, int [][] array) {
boolean flag = false;
int x=0;
int y=0;
for(int i=0;i < array.length;i++) {
for(int j=0;j<array[i].length;j++) {
if(target==array[i][j]){
flag = true;
break;
}
}
}
if(flag) {
System.out.println("exist!"+target+",location:"+"array["+x+"]["+y+"]");
}
return flag;
}
}

```

``````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;
}
}```

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

```public class Solution {
public boolean Find(int target, int [][] array) {
if(array == null || array[0].length == 0){
return false;
}
for(int i = 0;i < array.length;i++){
int tail = array[i].length-1;
if(target < array[i][mid]){
tail = mid - 1;
}
else if(target > array[i][mid]){
}
else
return true;
}
}
return false;
}
}
```

2098条回答 507717浏览

# 通过挑战的用户

• 2018-09-24 05:28:29
• 2018-09-24 01:55:42
• 2018-09-24 00:53:36
• 2018-09-24 00:51:26
• 2018-09-24 00:20:20

# 相关试题

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

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