给定一个的整形矩阵matrix和一个整数K, matrix的每一行和每一列都是排好序的。
实现一个函数,判断K是否在matrix中
[要求]
时间复杂度为,额外空间复杂度为。
第一行有三个整数N, M, K
接下来N行,每行M个整数为输入的矩阵
若K存在于矩阵中输出"Yes",否则输出"No"
2 4 5 1 2 3 4 2 4 5 6
Yes
2 4 233 1 2 3 4 2 4 5 6
No
import java.util.Scanner; public class Main{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); int K = sc.nextInt(); int[][]mat = new int[N][M]; for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ mat[i][j] = sc.nextInt(); } } boolean hasFound = false; int i = N - 1, j = 0; while(i >= 0 && j < M){ if (mat[i][j] > K) i--; else if (mat[i][j] < K) j++; else { hasFound = true; break; } } if (hasFound) System.out.println("Yes"); else System.out.println("No"); } }
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out)); String s1=reader.readLine(); String[] split1=s1.split(" "); int n=Integer.parseInt(split1[0]); int m=Integer.parseInt(split1[1]); int k=Integer.parseInt(split1[2]); int[][] nums=new int[n][m]; for(int i=0;i<n;i++){ String s2=reader.readLine(); String[] split2=s2.split(" "); for(int j=0;j<m;j++){ nums[i][j]=Integer.parseInt(split2[j]); } } int i=0,j=m-1; while(i<n&&j>=0){ if(nums[i][j]==k){ writer.write("Yes"); writer.flush(); return; }else if(nums[i][j]>k){ j--; }else{ i++; } } writer.write("No"); writer.flush(); writer.close(); reader.close(); } }
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int[][] nm = new int[n][m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ nm[i][j] = sc.nextInt(); } } for(int i = 0; i < n; i++){ if(k >= nm[i][0] && k <= nm[i][m-1]){ for(int j = 0 ; j < m; j++){ if(nm[i][j] == k){ System.out.println("Yes"); return; } } } } System.out.println("No"); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = sc.nextInt(); } } search(matrix, k); } private static void search(int[][] arr, int targt){ if(arr == null || arr.length < 1 || arr[0].length < 1){ System.out.println("No"); return; } int i = 0; int j = arr[0].length - 1; while(i <= arr[0].length - 1 && j >= 0){ if(arr[i][j] == targt){ System.out.println("Yes"); return; } if(arr[i][j] > targt){ j--; }else{ i++; } } System.out.println("No"); } }
一开始将指针定位在矩阵matrix的左上角,即i = 0, j = matrix[0].length - 1,当前元素为matrix[i][j]
matrix[i][j] < k时,i++,即指针向下移动;
matrix[i][j] > k时,j--,即指针向左移动;
matrix[i][j] == k时,说明找到了元素。
import java.util.Scanner; public class Main { public static boolean ifKInMatrix(int[][] matrix, int k) { int rows = matrix.length; int cols = matrix[0].length; int i = 0; int j = cols - 1; while (i < rows && j >= 0) { if (matrix[i][j] < k) { i++; } else if (matrix[i][j] > k) { j--; } else { return true; } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = sc.nextInt(); } } boolean res = ifKInMatrix(matrix, k); if (res) { System.out.println("Yes"); } else { System.out.println("No"); } } }
import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n=scanner.nextInt(),m=scanner.nextInt(),k=scanner.nextInt(); int[][] matrix = new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) matrix[i][j]=scanner.nextInt(); int i=0,flag=0; while(i<n&&m>0){ if(matrix[i][m-1]==k){ flag=1;break; }else if(matrix[i][m-1]<k) i++; else m--; } if(flag==1) System.out.println("Yes"); else System.out.println("No"); } }