运行时间:434ms
占用内存:19700k
二维数组中的查找
public class Solution {
public boolean Find(int target, int [][] array) {
if (array == null) {
return false;
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (target == array[i][j]) {
return true;
}
}
}
return false;
}
}
运行时间:404ms
占用内存:17980k
public class Solution {
public boolean Find(int target, int [][] array) {
boolean found = false;
if (array != null) {
int row = 0;
int rows = array.length;
int column = array[0].length - 1;
while (row < rows && column >= 0) {
if (array[row][column] == target) {
found = true;
break;
} else {
if (array[row][column] > target) {
column--;
} else {
row++;
}
}
}
}
return found;
}
}
网友评论