思路看注释
import java.io.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] arguements = reader.readLine().split(" "); int m = arguements[0].charAt(0) - '0'; int n = arguements[1].charAt(0) - '0'; int k = arguements[2].charAt(0) - '0'; int[][] a = new int[n][m]; String[] elements; for (int i = 0; i < n; i++){ elements = reader.readLine().split(" "); for(int j = 0; j < m; j++){ a[i][j] = elements[j].charAt(0) - '0'; } } if(isContains(a,k)){ System.out.print("Yes"); }else{ System.out.print("No"); } } public static boolean isContains(int[][] arr,int target){ //从右上角开始 int colIndex = arr[0].length-1; int rowIndex = 0; while(true){ if(arr[rowIndex][colIndex]==target){ //相等,找到了返回true return true; } if(arr[rowIndex][colIndex]>target){ //大于目标值,列向左移动 colIndex--; } if(arr[rowIndex][colIndex]<target){ //小于目标值,行向下移动 rowIndex++; } if(colIndex<0||rowIndex>arr.length-1){ //越界退出 break; } } return false; } }