星空网 > 软件开发 > Java

240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

 

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

 

For example,

Consider the following 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]]

Given target = 5, return true.

Given target = 20, return false.

代码如下:

 1 public class Solution { 2   public boolean searchMatrix(int[][] matrix, int target) { 3     List<Integer> list=new ArrayList<>(); 4     for(int i=0;i<matrix.length;i++) 5     { 6       for(int j=0;j<matrix[i].length;j++){ 7         list.add(matrix[i][j]); 8       } 9     }10     if(list.contains(target))11     return true;12     13     return false;14   }15 }

 




原标题:240. Search a 2D Matrix II

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

英国硬脱欧或提前,农民牵羊上街抗议!:https://www.ikjzd.com/articles/104716
中通快递第二季度净利润1.988亿美元!:https://www.ikjzd.com/articles/104717
Instagram将进军电商行业,开设购物账号shop!:https://www.ikjzd.com/articles/104718
姐夫将喜提哪两个城市作为第二总部拆分的备胎?:https://www.ikjzd.com/articles/10472
SEO纯干货:2019年亚马逊产品排名完整指南!:https://www.ikjzd.com/articles/104720
35个综试区还将新增一批跨境电商试点城市,落实“无票免税”政策:https://www.ikjzd.com/articles/104724
TikTok退出美国?商家:等等!:https://www.kjdsnews.com/a/1836334.html
越南是你的潜在市场吗?最全越南外贸市场概况!:https://www.kjdsnews.com/a/1836335.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流