LeetCode - 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
.
思路:
二分查找
package array;public class SearchA2DMatrix {public boolean searchMatrix(int[][] matrix, int target) {int m;int n;if (matrix == null || (m = matrix.length) == 0 || (n = matrix[0].length) == 0) return false;int start = 0;int end = m * n - 1;while (start <= end) {int mid = (end - start) / 2 + start;int i = mid / n;int j = mid - i * n;if (matrix[i][j] == target)return true;if (matrix[i][j] < target)start = mid + 1;elseend = mid - 1;}return false;}public static void main(String[] args) {// TODO Auto-generated method stubint[][] matrix = {{ 1, 3, 5, 7 },{ 10, 11, 16, 20 },{ 23, 30, 34, 50}};SearchA2DMatrix s = new SearchA2DMatrix();System.out.println(s.searchMatrix(matrix, 8));}}
转载于:https://www.cnblogs.com/null00/p/5093203.html
LeetCode - Search a 2D Matrix相关推荐
- LeetCode Search a 2D Matrix II
问题:给出一个二维数组,其行,列是递增的,和一个要查找的数,问矩阵中是否有要查找的数 思路: 第一种方法是直接暴力查找,在二维数组中查找. 第二种方法是基于对角线上,在行,列上作二分查找 第三种方法递 ...
- LeetCode: Search a 2D Matrix
少数次过 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int> > &matrix, ...
- LeetCode Search a 2D Matrix
// 68ms size 返回的是无符号整数,和负数比较时序特别注意转换 1 class Solution { 2 public: 3 bool searchMatrix(vector<vect ...
- 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...
- LeetCode 240. Search a 2D Matrix II
LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...
- LeetCode 74. Search a 2D Matrix
LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...
- leetcode 240. Search a 2D Matrix II | 240. 搜索二维矩阵 II(Java)
题目 https://leetcode.com/problems/search-a-2d-matrix-ii/ 题解 方法1 思路类似于 leetcode 200. Number of Islands ...
- leetcode 74 java_【LeetCode】74. Search a 2D Matrix
问题描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- LeetCode刷题(40)--Search a 2D Matrix
已经排好序的矩阵,搜索一个元素,二分法 class Solution(object):def searchMatrix(self, matrix, target):""" ...
最新文章
- Quartz定时框架CronTrigger开发使用实例
- WWW 2020 | 信息检索中的对话式问题建议
- [LintCode] Reverse Integer
- do { ....} while(0) 在宏里冗余的意义
- iOS开发之适配http请求
- voc2007,voc2012数据集快速下载方法
- ASP.NET Core 和 EF Core 系列教程——排序、筛选、分页和分组
- 全球红外(IR)LED行业收入预计2028年达到13.699亿美元
- oracle中execute函数,oracle中execute immediate的使用(select/insert/update/delete)详解
- 为什么对偶问题一定是凸优化问题?
- Mac下Tesseract-OCR文字识别新手使用入门
- 电脑用js调用QQ客服聊天 阿星小栈
- netty案例,netty4.1中级拓展篇五《基于Netty搭建WebSocket,模仿微信聊天页面》
- 嵌入式linux开发板使用pulseaudio连接蓝牙耳机播放音频文件
- 广州番禺翠湖山庄小区规划图
- 肥而不腻的红烧肉做法
- SSL_ERROR_BAD_CERT_DOMAIN 部署 ssl证书后仍显示潜在风险
- 教之初服务器管理系统使用,教之初题库管理系统操作教程-考题处理
- matlab汽车座椅脉冲振动冲击仿真
- gpu虚拟 服务器,5种GPU虚拟化技术的详细资料讲解
热门文章
- IDEA 快捷键 Android Studio快捷键
- 通过QEMU 和 IDA Pro远程调试设备固件
- 基于Android的ELF PLT/GOT符号重定向过程及ELF Hook实现(by 低端码农 2014.10.27)
- NOIP2018比赛总结
- android 隐藏webview地址栏,flutter - 在Flutter Webview中隐藏Url引用 - SO中文参考 - www.soinside.com...
- python绘制饼图explode_python通过matplotlib生成复合饼图
- pe如何自动加载外置工具_如何自动加载网站的深色模式?
- hdu5399(模拟)
- python大数字计算时没有响应_linux-为什么在尝试计算非常大的数字时Python会“抢先”挂起?...
- HDU - 2612 Find a way(BFS搜索)