资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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中怎么查找排序矩阵有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


新闻名称:LeetCode中怎么查找排序矩阵
网站网址:http://cdkjz.cn/article/jeissh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220