74. Search a 2D Matrix

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 from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ]
Given target = 3, returntrue.

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length,col = matrix[0].length;
        int i = 0,j = col-1;
        while(i<row&&j>=0)
        {
            if(target>matrix[i][j])
               i++;
            else if(target<matrix[i][j])
               j--;
            else
               return true;
        }
        return false;
        
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 清晨,独自行走,耳畔回想起“春有百花秋有月,夏有凉风冬有雪,若无闲事挂心头,便是人生好时节”一切都近乎是苦尽甘来,...
    往事随风梦余生阅读 3,715评论 1 2
  • 高考结束后绝对是人生最放松的阶段,休息了三个月已经可以把人歇懒了。 在没有离开那所在我看来极度无趣的高中时,我厌恶...
    狐肆月阅读 2,875评论 0 1
  • 一个人有多不正经,就有多深情,对他的喜欢始于穿透人心的情歌,陷于其出众的才华,忠于其高尚的人品。 做音乐,他一直是...
    请叫我刘小百阅读 3,207评论 4 6
  • "咪蒙"公众号 2017.03.29 原标题"有钱人终成眷属" 我来改标题 1.为什么有情人终究不能成眷属? 2....
    永利哥阅读 1,267评论 0 2