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 ...
随机推荐
- element-ui 时间日期选择器格式化后台需要的格式
<el-date-picker v-model="startTime" type="datetime" format="yyyy-MM-dd H ...
- SuperMap iClient for JavaScript image出图
SuperMap iClient for JavaScript 客户端基于openlayers 开发. 目前最高版本为811,9D产品后推荐客户使用leaflet.openlayers客户端开发. 问 ...
- HashMap 与 ConcrrentHashMap 使用以及源码原理分析
前奏一:HashMap面试中常见问题汇总 HashMap的工作原理是近年来常见的Java面试题,几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和Has ...
- Visualization of Detail Point Set by Local Algebraic Sphere Fitting
Refers to Dynamic Sampling and Rendering of Algebraic Point Set Surfaces Growing Least Squares for t ...
- Codeup
问题 I: 习题5-10 分数序列求和 时间限制: 1 Sec 内存限制: 12 MB提交: 611 解决: 537[提交][状态][讨论版][命题人:外部导入] 题目描述 有如下分数序列 求出次 ...
- CSS回顾(基础知识,元素,选择器,盒子,颜色)
元素分类: 1.行级元素:内联元素 inline 特征:内容决定元素所占位置,不可以通过CSS改变宽高 span strong em a del 2.块级元素:block特征:独占一行,可 ...
- 云ERP真的靠谱吗?
现在几乎每个IT系统或项目都要跟云挂上钩,跟数码产品必与“智能”扯上关系一样,否则在外行甚至同行眼里就是“矮小搓”.ERP领域也悄然刮起了云端化.国内ERP产品也借此机会想弯道超车,通过云化来抢夺被S ...
- [20181007]12cR2 Using SQL Patch 2.txt
[20181007]12cR2 Using SQL Patch 2.txt --//12cR2 已经把sql打补丁集成进入dbms_sqldiag,不是11g的 DBMS_SQLDIAG_INTERN ...
- 通过Socket让远程电脑执行脚本
实现功能: 客户端发送命令,服务器接收命令并执行 服务端: import socketserver, os class MyTCPHandler(socketserver.BaseRequestHan ...
- Hive中笔记 :三种去重方法,distinct,group by与ROW_Number()窗口函数
一.distinct,group by与ROW_Number()窗口函数使用方法 1. Distinct用法:对select 后面所有字段去重,并不能只对一列去重. (1)当distinct应用到多个 ...