leetcode — search-a-2d-matrix
/**
* Source : https://oj.leetcode.com/problems/search-a-2d-matrix/
*
*
* 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 from left to right.
* The first integer of each row is greater than the last integer of the previous row.
*
* For example,
*
* Consider the following matrix:
*
* [
* [1, 3, 5, 7],
* [10, 11, 16, 20],
* [23, 30, 34, 50]
* ]
*
* Given target = 3, return true.
*
*/
public class SearchMatrix {
/**
* 因为矩阵是连续有序的,所以可以当做一维数组处理,使用二分法搜索
* 也可以使用二分法先搜索第一列,确定处于哪一行,再对该行使用二分法搜索
*
*
*
* @return
*/
public boolean search (int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int left = 0;
int right = m * n;
int mid = (left + right) / 2;
int midi = mid / n;
int midj = mid % n;
while (left <= right) {
if (matrix[midi][midj] == target) {
return true;
}
if (matrix[midi][midj] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
mid = (left + right) / 2;
midi = mid / n;
midj = mid % m;
}
return false;
}
public static void main(String[] args) {
SearchMatrix searchMatrix = new SearchMatrix();
int[][] matrix = new int[][]{
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}
};
System.out.println(searchMatrix.search(matrix, 3));
System.out.println(searchMatrix.search(matrix, 11));
System.out.println(searchMatrix.search(matrix, 34));
System.out.println(searchMatrix.search(matrix, 0));
}
}
leetcode — search-a-2d-matrix的更多相关文章
- [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 搜索一个二维矩阵
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
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 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——Search a 2D Matrix
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 @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [Leetcode] search a 2d matrix 搜索二维矩阵
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 二分搜索
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,该列作废.这样 ...
随机推荐
- KVM 虚拟机的热迁移
热迁移:顾名思义在虚拟机不关机的情况下将KVM虚拟机进行迁移 准备工作:两台KVM虚拟机,一台nfs虚拟机,centos7.4系统 主机 IP地址 主机名 KVM01 10.00.11 kvm01 K ...
- Oracle 12c 安装问题及解决方案
1. 介绍 今天在我的开发电脑上安装Oracle12c,电脑环境是windows10家庭中文版,安装的Oracle数据库版本Oracle(12.1.0.2.0) - Standard Edition ...
- Java程序设计(第二版)复习 第三章
数组的使用 首先定义,然后用new生成数组,最后通过下标访问 定义 此时只是引用还未分配内存空间,需要使用new去分配内存空间,否则是无法被访问的 定义的两种方法:数据类型 数组名[];数据类型 [] ...
- JAVA 8 主要新特性 ----------------(七)新时间日期 API -----LocalDate
一.改版原因 1.老板的Date和Calander存在问题,日期操作名称混乱,有的在text下,有的在util下,包名混乱 2.Simple包混乱,致命错误线程不安全. ...
- HDU 4609 3-idiots (组合数学 + FFT)
题意:给定 n 条边,问随机选出 3 条边,能组成三角形的概率是多少. 析:答案很明显就是 能组成三角形的种数 / (C(n, 3)).现在的问题是怎么求能组成三角形的种数. 这个博客说的非常清楚了 ...
- Selenium定位iframe动态ID
Selenium定位iframe动态ID. 126邮箱实例 买了本虫师的书来学习selenium2自动化测试,然后写第一个实例就遇到了一些坑,好在有热心的网友提供了帮助,解决了问题 要学习seleni ...
- Jsp处理过程and数据交互
request处理客户端请求 客户端-------------->jsp页面--------------->服务器 常用方法 1.String getParameter(String na ...
- Java类更改常量后编译不生效
在Java文件中,指向编译时static final的静态常量, 会被在运行时解析为一个局部的常量值(也就是说静态常量在编译后,成为了常量,而不是原先的代码).这对所有的基础数据类型(就像int ,f ...
- .Net异步实例讲解
说起异步,Thread,Task,async/await,IAsyncResult 必须掌握 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部分可以同时执行:对于比较耗时的操作( ...
- docker容器下mysql更改WordPress的site address和home(URL)------局域网
先简单介绍下,用docker安装的WordPress,mysql是在docker容器中的,并未在Ubuntu(我把WordPress是安装Ubuntu系统上),即WordPress和Ubuntu是独立 ...