Search a 2D Matrix II
来源:互联网 发布:旭辉写频软件 编辑:程序博客网 时间:2024/06/07 00:02
Description:
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.
问题描述:
搜索二维矩阵I的变体,矩阵中每行元素递增,每列元素也是递增的。在矩阵中寻找目标元素。。。
解法一:
思路:
我们知道左上角一定是最小元素,右下角一定是最大元素。。由此可以从左下角元素与目标元素比较,
如果target > 则去掉这列
如果target < 则去掉这行
Code:
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if (matrix == null || matrix.length == 0){ return false; } if (matrix[0] == null || matrix[0].length ==0){ return false; } int row = matrix.length; int col = matrix[0].length; int x = row - 1; int y = 0; while (x >= 0 && y < col){ if (target == matrix[x][y]){ return true; } else if (target > matrix[x][y]){ y++; } else if (target < matrix[x][y]){ x--; } } return false; }}
0 0
- Search a 2D Matrix II
- [刷题]Search a 2D Matrix II
- lintcode:Search a 2D Matrix II
- #leetcode#Search a 2D Matrix II
- Search a 2D Matrix II
- [LeetCode] Search a 2D Matrix II
- leetcode: Search a 2D Matrix II
- leetcode - Search a 2D Matrix II
- Search a 2D Matrix II
- 240Search a 2D Matrix II
- [leetcode] Search a 2D Matrix II
- 【leetcode】Search a 2D Matrix II
- Search a 2D Matrix II
- Search a 2D Matrix II
- Search a 2D Matrix II
- Leetcode: Search a 2D Matrix II
- LeetCode Search a 2D Matrix II
- [Leetcode]Search a 2D Matrix II
- 数据结构实验之排序八:快速排序
- 程序员讨厌项目经理的10点
- BZOJ 2794 DP
- java 设计模式之装饰模式
- 2017年腾讯移动客户端开发岗位电话面试
- Search a 2D Matrix II
- 1878: [SDOI2009]HH的项链
- 【Mybatis】深入浅出Mybatis(七)——别名使用
- Android使用Retrofit+OkHttp实现网络请求
- 如何使用Spring 配置各类(c3p0,dbcp,jndi)数据源以及jdbc
- leetcode_middle_83_200. Number of Islands
- SQL常用命令
- 【leetcode】264. Ugly Number II
- 数据库事务的四大特性以及事务的隔离级别