二维数组中的查找
Array基本操作:
1.声明:int[] arr; int arr[]=new int[3];
2.初始化:int arr[]=new int[]{1,2,3,4,5}; 二维初始化: int[][] arr = new int[][]{{1,2}, {2, 3}, {4, 5}};(https://www.cnblogs.com/hujunzheng/p/3871899.html)
2.长度: arr.length; 二维数组的宽度:arr[0].length;
3.int数组转成string数组:int[] array3 = {1,2,3,4,5,6,7,8,9,0};
String arrStrings = Arrays.toString(array3);https://www.cnblogs.com/epeter/p/5664926.html
public class Solution { //与左下角元素相比,如果大就往右,小就往左
public boolean Find(int target, int [][] array) {
int i = array.length-1;
int j = 0;
while(i >= 0 && j < array[0].length){
if(target == array[i][j]) return true;
else if (target > array[i][j]){j++;}
else {i--;}
}
return false;
}
}
public boolean Find(int target, int [][] array) {
int i = array.length-1;
int j = 0;
while(i >= 0 && j < array[0].length){
if(target == array[i][j]) return true;
else if (target > array[i][j]){j++;}
else {i--;}
}
return false;
}
}
浙公网安备 33010602011771号