概述
package class_03;
/**
*
* 在行列都排好序的矩阵中找数
【题目】 给定一个有N*M的整型矩阵matrix和一个整数K,
* matrix的每一行和每一 列都是排好序的。实现一个函数,判断K
* 是否在matrix中。 例如: 0 1 2 5 2 3 4 7 4
* 4 4 8 5 7 7 9 如果K为7,返回true;如果K为6,返
* 回false。
【要求】 时间复杂度为O(N+M),额外空间复杂度为O(1)。
*
*/
public class Code_09_FindNumInSortedMatrix {
public static boolean isContains(int[][] matrix, int K) {
int row = 0;
int col = matrix[0].length - 1;
while (row < matrix.length && col > -1) {
if (matrix[row][col] == K) {
return true;
} else if (matrix[row][col] > K) {
col--;
} else {
row++;
}
}
return false;
}
public static void main(String[] args) {
int[][] matrix = new int[][] { { 0, 1, 2, 3, 4, 5, 6 },// 0
{ 10, 12, 13, 15, 16, 17, 18 },// 1
{ 23, 24, 25, 26, 27, 28, 29 },// 2
{ 44, 45, 46, 47, 48, 49, 50 },// 3
{ 65, 66, 67, 68, 69, 70, 71 },// 4
{ 96, 97, 98, 99, 100, 111, 122 },// 5
{ 166, 176, 186, 187, 190, 195, 200 },// 6
{ 233, 243, 321, 341, 356, 370, 380 } // 7
};
int K = 233;
System.out.println(isContains(matrix, K));
}
}
最后
以上就是包容蜜粉为你收集整理的在行列都排好序的矩阵中找数【要求】 时间复杂度为O(N+M),额外空间复杂度为O(1)。的全部内容,希望文章能够帮你解决在行列都排好序的矩阵中找数【要求】 时间复杂度为O(N+M),额外空间复杂度为O(1)。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复