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 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 Solution {
- public boolean searchMatrix(int[][] m, int target) {
- //两个二分搜索
- //先搜索第一列,找到确定的行,然后再搜索行
- int i = 0;
- int j = m.length-1;
- int mid = 0;
- //搜寻第一列
- while(i <= j){
- mid = (i + j)/2;
- if(m[mid][0] == target){
- return true;
- }else if(m[mid][0] < target){
- i = mid + 1;
- }else{
- j = mid - 1;
- }
- }
- if(m[mid][0] > target){
- if(mid == 0){
- return false;
- }
- mid--;//mid-1
- }
- //搜寻mid行
- i = 0;
- j = m[0].length -1;
- int k = mid;
- //搜寻到了返回true
- while(i <= j){
- mid = (i + j)/2;
- if(m[k][mid] == target){
- return true;
- }else if(m[k][mid] < target){
- i = mid + 1;
- }else{
- j = mid - 1;
- }
- }
- //没有搜寻到,返回false
- return false;
- }
- }
leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法的更多相关文章
- [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 ...
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
- Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...
- [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 follo ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [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 follo ...
- 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 follo ...
- search a 2D matrix(在二维数组中搜索一个元素)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- 分别使用Hadoop和Spark实现TopN(1)——唯一键
0.简介 TopN算法是一个经典的算法,由于每个map都只是实现了本地的TopN算法,而假设map有M个,在归约的阶段只有M x N个,这个结果是可以接受的并不会造成性能瓶颈. 这个TopN算法在ma ...
- js数据管理的思考
最近要做一个农场项目,涉及到很多js数据管理的需求,这里也做下总结,不断的总结,再修正内容,也是快速进步的方法. 数据管理几个方面考虑: * 设置(更新)字段值 * 检索,根据id, index, 属 ...
- 最简单的多线程死锁案例代码(Java语言)
package com.thread.test; public class DeadLock { private static Object firstMonitor = new Object(); ...
- classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList();
package a927; import java.util.ArrayList; import java.util.List; class Student { private String name ...
- Linux学习自动化脚本(一)
https://www.cnblogs.com/handsomecui/p/5869361.html https://blog.csdn.net/daigualu/article/details/76 ...
- ROS:Nvidia Jetson TK1平台安装使用ROS
原文连接: http://wiki.ros.org/indigo/Installation/UbuntuARM Ubuntu ARM install of ROS Indigo There are c ...
- 通用功能类:改变WinForm窗体显示颜色
一.显示窗体调用方法 protected override void OnLoad(EventArgs e) { MDIClientSupport.SetBevel ...
- sql 排序
select count(*) from vote group by contents PERCENT * from vote order by contents)as A group by cont ...
- scrapy获取重定向之前的url
通过 response.request.meta['redirect_urls'] 来获取跳转之前的链接
- esp32(M5STACK)在线体验(Ubuntu)
我们往m5stack烧录的固件是可以在线编程的 具体使用方法可以参考 https://github.com/m5stack/M5Cloud/blob/master/README_CN.md ...