- 浏览: 185672 次
- 性别:
- 来自: 济南
文章分类
最新评论
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)。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 271Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 275You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 392Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 380Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 506Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 573Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 484Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 674Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 478The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 437Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 585Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 596Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 432All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 909Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 936Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 609Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 705Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 870Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 796You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 735For a undirected graph with tre ...
相关推荐
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"是一道非常经典的编程问题,它涉及到二维矩阵的操作和二分查找算法的应用。本题目的目标是设计一个高效的算法,在一个由整数组成的矩阵中查找指定的目标值,如果找到,返回其...
python python_leetcode题解之074_Search_a_2D_Matrix
javascript js_leetcode题解之74-search-a-2d-matrix.js
c c语言_leetcode题解之0074_search_a_2d_matrix.zip
c语言入门 C语言_leetcode题解之74-search-a-2d-matrix.c
- Search a 2D Matrix II(二维矩阵中的搜索II) - Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array(在旋转排序数组中搜索) - Search in Rotated Sorted Array II(在旋转排序数组中搜索...
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/...
Search a 2D Matrix II 替换空格 从尾到头打印链表 重建二叉树 105. Construct Binary Tree from Preorder and Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. ...
LeetCode判断字符串是否循环 LC ...search a 2d matrix ii. 每次仅能将搜索区域缩减为之前的3/4,效率一般。从左下角或右上角扫描矩阵,时间复杂度为O(m+n)代码简单,且有较高的效率。 perfect squa
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
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
第74题,名为“Search a 2D Matrix”(搜索二维矩阵),是其中的一道经典问题。这道题目要求你在一个二维矩阵中查找一个特定的元素。让我们深入探讨这个问题,以及如何用C++来解决它。 首先,二维矩阵是C++中常见的...
* Search a 2D Matrix:该题目要求在二维矩阵中查找元素,实现方法使用了二分查找算法。 * Rotate Image:该题目要求将二维矩阵旋转90度,实现方法使用了循环移位的方法。 五、其他 * Set Matrix Zeroes:该题目...
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 ...
这是我准备面试时建议的个人问题清单。... https://leetcode.com/problems/search-a-2d-matrix/ https://leetcode.com/problems/set-matrix-zeroes/ 弦乐 https://leetcode.com/problems/positions-of-large-g
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:二维网格的简单深度优先遍历"是这个话题的一个具体实例,它涉及到如何在一个给定大小的2D矩阵(网格)中找到从左上角到右下角的所有可能路径。这里我们将深入探讨这个问题,并通过JavaScript语言来...
- 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 ...