leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix
整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了。
这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,%col获得y坐标
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
if(row <= )
return false;
int col = matrix[].size();
if(col <= )
return false;
int start = ;
int end = row*col - ;
int mid;
while(start + < end){
mid = start + (end - start)/;
int r = mid/col;
int c = mid%col;
if(matrix[r][c] == target)
return true;
else if(matrix[r][c] < target)
start = mid;
else
end = mid;
}
if(matrix[start/col][start%col] == target)
return true;
if(matrix[end/col][end%col] == target)
return true;
return false;
}
};
240. Search a 2D Matrix II
与第一个题不同,行与行之间不是严格递增。剑指上的原题,从最左下角或者最右上角都可以
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if(m <= )
return false;
int n = matrix[].size();
if(n <= )
return false;
int i = ,j = n-;
while( i < m && j >= ){
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] > target)
j--;
else
i++;
}
return false;
}
};
leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II的更多相关文章
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [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] 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
随机推荐
- vue单页应用添加百度统计
前言 申请百度统计后,会得到一段JS代码,需要插入到每个网页中去,在Vue.js项目首先想到的可能就是,把统计代码插入到index.html入口文件中,这样就全局插入,每个页面就都有了;这样做就涉及到 ...
- Web标准中xhtml规范的内容有哪些
1.所有的标记都必须要有一个相应的结束标记 以前在HTML中,你可以打开许多标签,例如<p>和<li>而不一定写对应的</p>和</li>来关闭它们.但 ...
- 使用vmimeNET解析账单邮件
大概所有做APP的公司都是不愿意做自定义的,哪怕自己的功能再烂也愿意慢慢修补不愿意开源一部分. 卡牛- 51信用卡- 一次次的逾期 自己写个信用卡管理工具,从邮件中提取账单,还款后做个登记,到了还 ...
- JMeter 报告监听器导入.jtl结果文件报错解决方案
JMeter 报告监听器导入.jtl结果文件报错解决方案 by:授客 QQ:1033553122 1. 问题描述 把jmeter压测时生成的 .jtl结果文件导入监听器报告中,弹出如下错误提示 ...
- matlab练习程序(马尔可夫聚类MCL)
本文主要参考: https://wenku.baidu.com/view/b7907665caaedd3383c4d31b.html https://blog.csdn.net/u010376788/ ...
- python变量类型&字符串的内建函数使用
python常用数据类型: 数字(整数int,浮点数float) 字符串 元组 列表 字典 一.格式化输出 1.1第一种格式化输出 %d整数 %f浮点数(用.*表示精确到多少位小数) %s字符串 % ...
- Android Thread 官方说明
Thread官方说明 https://developer.android.google.cn/reference/java/lang/Thread Thread是程序中执行的线程.Java虚拟机允许应 ...
- openstack Ocata版本 python
from keystoneauth1.identity import v3 from keystoneauth1 import session from novaclient import clien ...
- SQL SERVER数据库级的触发器
CREATE TRIGGER [Object_Change_Trigger_DDL] ON database FOR DROP_TABLE AS DECLARE @EventData AS xml; ...
- (转载)关于usr/bin/ld: cannot find -lxxx问题总结
usr/bin/ld: cannot find -lxxx问题总结 linux下编译应用程序常常会出现如下错误: /usr/bin/ld: cannot find -lxxx 意思是 ...