`

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.

在一个二维数组找一个元素,数组中的每一行和每一列都是升序排列的。我们可以从数组的右上方作者左下方的元素开始寻找。设定两个指针m, n,假设从右上方开始,如果matrix[m][n]与target相等就返回true;如果matrix[m][n]小于target就让m指针往下移动(m++); 如果大于target就让n指针向左移动(n--), 直到遍历完整个数组,这样时间复杂度为O(m+n)。代码如下:
public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix == null || matrix.length == 0) return false;
        int m = 0;
        int n = matrix[0].length - 1;
        while(m < matrix.length && n >= 0) {
            if(matrix[m][n] == target) return true;
            if(matrix[m][n] > target) {
                n --;
            } else {
                m ++;
            }
        }
        return false;
    }
}
0
2
分享到:
评论

相关推荐

    lrucacheleetcode-Coding-Interview:编程面试

    A repo for popular coding interview problems mainly from Leetcode. 二分搜索/有序数组旋转 Find Minimum In Rotated Sorted Array Find Minimum In Rotated Sorted Array II Search In Rotated Sorted Array ...

    leetcode_Search-a-2D-Matrix.zip_leetcode

    在LeetCode平台上,"Search a 2D Matrix"是一道非常经典的编程问题,它涉及到二维矩阵的操作和二分查找算法的应用。本题目的目标是设计一个高效的算法,在一个由整数组成的矩阵中查找指定的目标值,如果找到,返回其...

    python-leetcode题解之074-Search-a-2D-Matrix

    python python_leetcode题解之074_Search_a_2D_Matrix

    js-leetcode题解之74-search-a-2d-matrix.js

    javascript js_leetcode题解之74-search-a-2d-matrix.js

    c语言-leetcode题解之0074-search-a-2d-matrix.zip

    c c语言_leetcode题解之0074_search_a_2d_matrix.zip

    C语言-leetcode题解之74-search-a-2d-matrix.c

    c语言入门 C语言_leetcode题解之74-search-a-2d-matrix.c

    算法刷题笔记leetcode/lintcode

    - Search a 2D Matrix II(二维矩阵中的搜索II) - Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array(在旋转排序数组中搜索) - Search in Rotated Sorted Array II(在旋转排序数组中搜索...

    LeetCode最全代码

    462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...

    Leetcode扑克-coding-interviews:编码面试

    Search a 2D Matrix II 替换空格 从尾到头打印链表 重建二叉树 105. Construct Binary Tree from Preorder and Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. ...

    LeetCode判断字符串是否循环-lc:液晶显示器

    LeetCode判断字符串是否循环 LC ...search a 2d matrix ii. 每次仅能将搜索区域缩减为之前的3/4,效率一般。从左下角或右上角扫描矩阵,时间复杂度为O(m+n)代码简单,且有较高的效率。 perfect squa

    LeetCode-NoteBook:docsifyjs

    LeetCode笔记本... Search a 2D Matrix II2. Add Two Numbers50. Pow(x, n)34. First & LastPositionElementInSortedArr94. Binary Tree Inorder Traversal144. Binary Tree Preorder Traversal145. Binary Tree

    LeetCode C++全解

    1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array iii.... Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element

    c++-c++编程基础之leetcode题解第74题搜索二维矩阵.zip

    第74题,名为“Search a 2D Matrix”(搜索二维矩阵),是其中的一道经典问题。这道题目要求你在一个二维矩阵中查找一个特定的元素。让我们深入探讨这个问题,以及如何用C++来解决它。 首先,二维矩阵是C++中常见的...

    LeetCode题解(java语言实现).pdf

    * Search a 2D Matrix:该题目要求在二维矩阵中查找元素,实现方法使用了二分查找算法。 * Rotate Image:该题目要求将二维矩阵旋转90度,实现方法使用了循环移位的方法。 五、其他 * Set Matrix Zeroes:该题目...

    leetcode答案-leetcode-java:leetcode的Java代码

    Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree ...

    recommended-problems

    这是我准备面试时建议的个人问题清单。... https://leetcode.com/problems/search-a-2d-matrix/ https://leetcode.com/problems/set-matrix-zeroes/ 弦乐 https://leetcode.com/problems/positions-of-large-g

    4:二维数组中的查找(剑指offer第2版Python)

    1、题目详解 ...https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/er-fen-fa-pai-chu-fa-python-dai-ma-java-dai-ma-by-/ 2、代码详解 # -*- coding:utf-8 -*- class Solution: # arr

    matrix-paths:二维网格的简单深度优先遍历

    "matrix-paths:二维网格的简单深度优先遍历"是这个话题的一个具体实例,它涉及到如何在一个给定大小的2D矩阵(网格)中找到从左上角到右下角的所有可能路径。这里我们将深入探讨这个问题,并通过JavaScript语言来...

    C++深度遍历

    - Uses a 2D array `a` to represent the adjacency matrix of the graph. - Initializes the `AlGraph` structure `G`. - **Edge Construction:** - For each vertex `i`, creates an adjacency list by adding ...

Global site tag (gtag.js) - Google Analytics