LeetCode OJ-74.Search a 2D Matrix
来源:互联网 发布:浙江人事考试机考网络 编辑:程序博客网 时间:2024/06/08 17:17
LeetCode OJ-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
, returntrue
.Subscribe to see which companies asked this question
题目理解
在一个二维矩阵中,求解target是否存在。可以将二维转化为一维,在一维数组中去寻找。时间复杂度为O(m * n),空间复杂度为O(1)。具体代码如下:
Code
bool search_matrix(vector<vector<int>> &matrix, int target){ int row = (int) matrix.size(); int col = (row > 0) ? (int) matrix[0].size() : 0; //判断是否是空vector int sz = row * col; int i; for (i = 0; i < sz; ++i) { if (matrix[i / col][i % col] == target) { //将二维转化为一维 return true; } } return false;}
0 0
- LeetCode OJ:Search a 2D Matrix
- LeetCode OJ - Search a 2D Matrix
- [LeetCode OJ]Search a 2D Matrix
- LeetCode OJ Search a 2D Matrix
- LeetCode OJ | Search a 2D Matrix
- LeetCode OJ-74.Search a 2D Matrix
- leetcode oj java Search a 2D Matrix
- leetcode oj java Search a 2D Matrix II
- LeetCode OJ 240. Search a 2D Matrix II
- LeetCode 74. Search a 2D Matrix
- [LeetCode]74.Search a 2D Matrix
- LeetCode --- 74. Search a 2D Matrix
- [Leetcode] 74. Search a 2D Matrix
- [leetcode] 74.Search a 2D Matrix
- Leetcode 74. Search a 2D Matrix
- [LeetCode]74. Search a 2D Matrix
- 74. Search a 2D Matrix LeetCode
- [LeetCode]74. Search a 2D Matrix
- oracle--plsql1---基础概念
- 欢迎使用CSDN-markdown编辑器
- PHP请求远程地址设置超时时间
- Android开发之---两个Activity之间利用广播机制更新数据
- 2个应用Log4j日志记录同一目录下问题
- LeetCode OJ-74.Search a 2D Matrix
- WCF常见问题记忆
- 迟来的2016总结
- Ubuntu 14 Caffe配置教程(cuda7.5+cuDnn v5)
- [bzoj4710][JSOI2011]分特产
- [Gradle中文教程系列]-跟我学Gradle-5.7:依赖-上传与发布番外篇-发布到Jcenter
- HDU - 3709 数位dp
- The zen of kobjects (linux kobjects 要旨)
- linux终端快捷键