一、題目:二維數組中的查找
題目:在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
例如下面的二維數組就是每行、每列都遞增排序。如果在這個數組中查找數字7,則返回true;如果查找數字5,由於數組不含有該數字,則返回false。
二、解題思路
首先選取數組中右上角的數字。如果該數字等於要查找的數字,查找過程結束;如果該數字大於要查找的數字,剔除這個數字所在的列;如果該數字小於要查找的數字,剔除這個數字所在的行。也就是說如果要查找的數字不在數組的右上角,則每一次都在數組的查找範圍中剔除一行或者一列,這樣每一步都可以縮小查找的範圍,直到找到要查找的數字,或者查找範圍爲空。
例如,我們要在上述的二維數組中查找數字7的步驟如下圖所示:
三、解決問題
3.1 代碼實現
public static boolean Find(int target, int [][] array) {
if (null == array || array.length == 0 || array[0].length == 0){
return false;
}
//二維數組的行數
int rows = array.length - 1;//爲了定位找到最右上角元素
int columns = array[0].length;
int column = 0;
while ((rows >= 0) && (column < columns)){
//如果選中的右上角元素元素 > 要查找的數字
//剔除該元素所在的列- rows--
if (array[rows][column] > target){
rows--;
//如果選中的右上角元素元素 < 要查找的數字
//剔除該元素所在的行- column++
}else if (array[rows][column] < target){
column++;
}else {
//如果選中的右上角元素元素 = 要查找的數字
//即找到了
return true;
}
}
//否則,就是沒找到
return false;
}
在前面的分析中,我們每一次都是選取數組查找範圍內的右上角數字。同樣,我們也可以選取左下角的數字。但我們不能選擇左上角或者右下角。以左上角爲例,最初數字1位於初始數組的左上角,由於1小於7,那麼7應該位於1的右邊或者下邊。此時我們既不能從查找範圍內剔除1所在的行,也不能剔除1所在的列,這樣我們就無法縮小查找的範圍。
選擇右上角比較方便
3.2 單元測試
package SwordOffer;
import org.junit.Test;
/**
* @author kankan
* @creater 2019-10-17 8:51
*/
public class Solution1 {
public static boolean Find(int target, int [][] array) {
if (null == array || array.length == 0 || array[0].length == 0){
return false;
}
//二維數組的行數
int rows = array.length - 1;//爲了定位找到最右上角元素
int columns = array[0].length;
int column = 0;
while ((rows >= 0) && (column < columns)){
//如果選中的右上角元素元素 > 要查找的數字
//剔除該元素所在的列- rows--
if (array[rows][column] > target){
rows--;
//如果選中的右上角元素元素 < 要查找的數字
//剔除該元素所在的行- column++
}else if (array[rows][column] < target){
column++;
}else {
//如果選中的右上角元素元素 = 要查找的數字
//即找到了
return true;
}
}
//否則,就是沒找到
return false;
}
//(1)要查找的數字在數組中
@Test
public void FindTest1(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(7, matrix));
}
//(2)要查找的數不在數組中
@Test
public void FindTest2(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(5, matrix));
}
//(3)要查找的數是數組中最小的數字
@Test
public void FindTest3(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(1, matrix));
}
//(4)要查找的數是數組中最大的數字
@Test
public void FindTest4(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(15, matrix));
}
//(5)要查找的數比數組中最小的數字還小
@Test
public void FindTest5(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(0, matrix));
}
//(6)要查找的數比數組中最大的數字還大
@Test
public void FindTest6(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(16, matrix));
}
//(7)魯棒性測試,輸入空指針
@Test
public void FindTest7(){
System.out.println(Solution1.Find(16, null));
}
}
努力也是需要學習的,別再讓你的努力,只感動了自己!願你的每一次努力,都能爲自己和別人創造價值。