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");

    }
}
全部评论

相关推荐

后端转测开第一人:再怎么劝退也没用的 某些群体总以为在一个幸存者偏差的软件上看见了极少数秋招上岸某个大厂的个例就幻想上了 事实上自己打开ssob沟通1000+连个小厂面试都没
点赞 评论 收藏
分享
哇哇的菜鸡oc:他这不叫校招offer,而是实习offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务