(medium)LeetCode 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.
解法一:暴力解法,遍历二维数组。
代码如下:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int rows=matrix.length;
int columns=matrix[0].length;
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++){
if(target==matrix[i][j])
return true;
}
return false;
}
}
运行结果:时间复杂度为O(M*N)

方法二:根据矩阵的特点,从右上角元素开始比较,target小,则列数减1,target大,则行数加1.
代码如下:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null ||matrix.length<1 || matrix[0].length<1){
return false;
}
int col=matrix[0].length-1;
int row=0;
while(col>=0 && row<=matrix.length-1){
if(target==matrix[row][col]){
return true;
}else if(target<matrix[row][col]){
col--;
}else if(target>matrix[row][col]){
row++;
}
}
return false;
}
}
运行结果:时间复杂度O(M+N)

(medium)LeetCode 240.Search a 2D Matrix II的更多相关文章
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Leetcode 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 follo ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- 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】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】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- 240 Search a 2D Matrix II 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...
随机推荐
- [原]网络库libevent在Visual Studio中的使用方法
libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名分布式缓存软件memcached也 ...
- 设置JSP不做浏览器缓存
CacheFilter代码: package com.my.filter; import java.io.*; import javax.servlet.*; import javax.servlet ...
- request的生命周期
有如下功能: 从index.jsp页面点击超链接进入TestServlet服务器,TestServlet服务器再请求转发到test.jsp. 在index.jsp里设置了request的attribu ...
- makefile学习笔记(多目录嵌套调用、变量使用)
http://blog.csdn.net/leexiang_han/article/details/9274229 学习了几天的makefile的嵌套调用编写也有一些心得,先声明,我也是初学者写文 ...
- C#全角和半角转换
在计算机屏幕上,一个汉字要占两个英文字符的位置,人们把一个英文字符所占的位置称为"半角",相对地把一个汉字所占的位置称为"全角".在汉字输入时,系统提供&quo ...
- PHP中的中文截取乱码问题_gb2312_utf-8
一.字符串编码为gb2312,一个中文占俩字节 public static function chinesesubstr($str, $start, $len) { // $str指字符串,$star ...
- 黄聪:深入理解PHP Opcode缓存原理
什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译,减少 ...
- iphone Dev 开发实例10:How To Add a Slide-out Sidebar Menu in Your Apps
Creating the Xcode Project With a basic idea about what we’ll build, let’s move on. You can create t ...
- PLSQL_Oracle簇表和簇表管理Index clustered tables(案例)
2012-06-08 Created By BaoXinjian
- unity c#
gameObject //获取当前脚本挂载到的游戏对象 在Unity中就算使用了C#进行编写脚本,要输出时不能使用Console类,应当使用print();或者Debug.log(); transfo ...