LeetCode中怎么查找排序矩阵
今天就跟大家聊聊有关LeetCode中怎么查找排序矩阵,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站设计、铜山网络推广、重庆小程序开发、铜山网络营销、铜山企业策划、铜山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供铜山建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
1,问题简述
给定M×N矩阵,每一行、每一列都按升序排列,请编写代码找出某元素。
2,示例
示例:现有矩阵 matrix 如下:[[1, 4, 7, 11, 15],[2, 5, 8, 12, 19],[3, 6, 9, 16, 22],[10, 13, 14, 17, 24],[18, 21, 23, 26, 30]]给定 target = 5,返回 true。给定 target = 20,返回 false。
3,题解思路
正常的题解思路
4,题解程序
public class SearchMatrixTest2 {public static void main(String[] args) {int[][] matrix = {{1, 4, 7, 11, 15},{2, 5, 8, 12, 19},{3, 6, 9, 16, 22},{10, 13, 14, 17, 24},{18, 21, 23, 26, 30}};int target = 5;boolean searchMatrix = searchMatrix(matrix, target);System.out.println("searchMatrix = " + searchMatrix);}public static boolean searchMatrix(int[][] matrix, int target) {if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {return false;}int rowLength = 0;int colLength = matrix[0].length - 1;while (rowLength < matrix.length && colLength >= 0) {if (matrix[rowLength][colLength] == target) {return true;} else if (matrix[rowLength][colLength] > target) {colLength--;} else {rowLength++;}}return false;}}
5,题解程序图片版

看完上述内容,你们对LeetCode中怎么查找排序矩阵有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
本文名称:LeetCode中怎么查找排序矩阵
网站链接:http://www.jxjierui.cn/article/jeissh.html


咨询
建站咨询
