java 从右上边找就行,类似二分

在行列都排好序的矩阵中找指定的数

http://www.nowcoder.com/questionTerminal/b929be9dbbaa489a91afa3fec195c228

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[][] mat = new int[N][M];
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++){
                mat[i][j] = sc.nextInt();
            }
        }

        int x = 0; 
        int y = M - 1;
        while(x < N && y >= 0){
            if(mat[x][y] == K){
                System.out.println("Yes");
                return;
            }else if(mat[x][y] < K){
                x++;
            }else{
                y--;
            }
        }
        System.out.println("No");

    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务