在行列都排好序的矩阵中找指定的数
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException{
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=bufr.readLine();
String[] tmp=line.split(" ");
int m=Integer.parseInt(tmp[0]);
int n=Integer.parseInt(tmp[1]);
int k=Integer.parseInt(tmp[2]);
int[][] arr=new int[m][n];
for(int i=0;i<m;i++) {
String[] line1=bufr.readLine().split(" ");
for(int j=0;j<n;j++) {
arr[i][j]=Integer.parseInt(line1[j]);
}
}
bufr.close();
System.out.println(isComp(arr,k,0,n-1)?"Yes":"No");
}
private static boolean isComp(int[][] arr,int key,int rows,int cols) {
if(arr==null||arr.length==0) {
return false;
}
while(rows<arr.length&&cols>=0) {
if(arr[rows][cols]>key) {
cols--;
}else if(arr[rows][cols]<key) {
rows++;
}else {
return true;
}
}
return false;
}
} 这道题很简单,也是剑指offer上的一道题,但是输入输出格式还有待熟悉。 
京公网安备 11010502036488号