首页 > 试题广场 >

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

[编程题]在行列都排好序的矩阵中找指定的数
  • 热度指数:23740 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个的整形矩阵matrix和一个整数K, matrix的每一行和每一列都是排好序的。
实现一个函数,判断K是否在matrix中
[要求]
时间复杂度为,额外空间复杂度为

输入描述:
第一行有三个整数N, M, K
接下来N行,每行M个整数为输入的矩阵


输出描述:
若K存在于矩阵中输出"Yes",否则输出"No"
示例1

输入

2 4 5
1 2 3 4
2 4 5 6

输出

Yes
示例2

输入

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

发表于 2022-05-23 16:43:45 回复(0)
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();
    }
}

发表于 2022-05-08 15:56:39 回复(0)
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");
    }
}

发表于 2022-03-18 21:31:34 回复(0)
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");
    }
} 

发表于 2021-09-22 20:43:36 回复(0)

Java解法

思路

一开始将指针定位在矩阵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");
        }
    }
}
发表于 2019-10-10 16:18:40 回复(1)
根据图解大佬的解题思路终于做出来了(笑哭)
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");
    }
}

发表于 2019-09-07 11:17:40 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
         Scanner s = new Scanner(System.in);
         int N = s.nextInt();
         int M = s.nextInt();
         int K = s.nextInt();
//         System.out.print(N + " " + M + " " + K);
         int[][] array = new int[N][M];
         for (int i = 0;i < N;i++) {
             for (int j = 0;j < M;j++) {
                 array[i][j] = s.nextInt();
             }
         }
        //根据矩阵的特性从右上角这个数开始查找,逼近
         int r = 0;
         int l = M - 1;
         int find_flag = 0;
         while (r < N && l > -1) {
             if (K == array[r][l]) {
                 find_flag = 1;
                 System.out.print("Yes");
             }
             if (K > array[r][l]) { //若大于右上角这个数,则这一行不用找了,直接换行
                 r++;
             }else if (K < array[r][l]){                //若小于右上角这个数,则这一列不用找了,直接换列
                 l--;
             }else if (K == array[r][l]) {
                 System.out.print("Yes");
                 return;
             }
         }
         System.out.print("No");
    }
}
可是最后只通过了40%,不知道什么原因,希望大佬们指点一下

发表于 2019-08-15 16:03:04 回复(1)

问题信息

上传者:小小
难度:
7条回答 11837浏览

热门推荐

通过挑战的用户

查看代码