LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/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.
题解:
本题是每一行每一列都是升序,从右上角开始x = matrix[i][j], i = 0, j = matrix[0].length - 1, 用x 和target 比较,若是相等则返回true.
若是小于target, 说明肯定不会在这一行,所以i++. 若是大于target, 说明必然不会在这一列所以 j--.
若走到边界还没有返回说明没有这个target, 返回false.
Note: j的初始量是 j = n-1, 注意减一.
Time Complexity: O(m+n). m = matrix.length. n = matrix[0].length.
Space: O(1).
AC Java:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int i = 0;
int j = n-1; //error
while(i<m && j>=0){
int x = matrix[i][j];
if(x == target){
return true;
}else if(x < target){
i++;
}else{
j--;
}
}
return false;
}
}
跟上Kth Smallest Element in a Sorted Matrix.
LeetCode Search a 2D Matrix II的更多相关文章
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
- LeetCode——Search a 2D Matrix II
Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
随机推荐
- lucene 使用注意
1.建立索引时,忘记writer.close(); 结果: 正常结果:
- storm在windows系统下安装调试
基础知识 Storm是一个分布式的,可靠的,容错的数据流处理系统.它会把工作任务委托给不同类型的组件,每个组件负责处理一项简单特定的任务.Storm集群的输入流由一个被称作spout的组件管理,spo ...
- 安装pypcap = 安装flex:the fast lexical analyser + 安装libpcap-1.7.4
flex flex is a tool for generating scanners 安装flex-2.6.0安装包 网址:https://sourceforge.net/projects/flex ...
- 关于Shader的跨平台方案的考虑
Apple 推出 metal后,除了新的metal framewrok外,也多了一种新的shader语言,最近工作也做了一些metal移植的测试,主要还是现有引擎如何可以快速支持metal的解决方 ...
- 推荐的PHP编码规范
推荐的PHP编码规范 发布时间: 2014-05-7 浏览次数:2754 分类: PHP教程 推荐的PHP编码规范 一 编辑器设置 1. 使用Tab缩进,不要使用空格 鉴于很多编辑器在保存文件时会自动 ...
- LR中HTTP协议录制模式选择
在LR中使用HTML/HTTP协议进行脚本录制时面临正确选择HTTP-based script / URL-base script 录制模式的问题,以下是比较官方的建议:1)基于浏览器的应用程序推荐使 ...
- mysql从只有一个备份文件(多个数据库的备份)中恢复数据到指定数据库
mysql -uroot -p 要恢复的数据库的名字 --one-database<备份文件
- 关于集合的练习P235-1,2,3
第一题: import java.util.*; public class ListTest { public static void main(String[] args) { ArrayList& ...
- P1443 马的遍历
同样是一个bfs水题... #include <bits/stdc++.h> using namespace std; typedef pair<int, int> St; S ...
- Tuning Spark
https://spark.apache.org/docs/1.2.1/tuning.html Data Serialization 数据序列化,对于任意分布式系统都是性能的关键点 Spark默认使用 ...