无标题文章

2017-09-20  本文已影响0人  柳仁儿

运行时间: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;
}
}

上一篇下一篇

猜你喜欢

热点阅读