当前位置:网站首页>剑指 Offer 04. 二维数组中的查找
剑指 Offer 04. 二维数组中的查找
2022-06-25 15:32:00 【anieoo】

solution:
暴力模拟,时间复杂度O(m * n)
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
for(auto &x : matrix)
for(auto &y : x) {
if(y == target) return true;
}
return false;
}
};逐行进行二分查找时间复杂度O(nlogn)
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
int n = matrix.size();
if (matrix.size() == 0 || matrix[0].size() == 0)
return false;
for(int i = 0;i < n;i++) {
if(binSearch(matrix[i], target)) return true;
}
return false;
}
bool binSearch(vector<int> &nums, int target) {
int l = 0,r = nums.size() - 1;
while(l < r) {
int mid = l + (r - l) / 2;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}
if(nums[l] == target) return true;
return false;
}
};从右上角开始查找,如果matrix[x][y] > target,y++,else x--
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
if(matrix.size() == 0 || matrix[0].size() == 0) return false;
int n = matrix.size(),m = matrix[0].size();
int x = n - 1,y = 0;
while(x >= 0 && y < m) {
if(matrix[x][y] == target) return true;
if(matrix[x][y] < target) y++;
else x--;
}
return false;
}
};
边栏推荐
- System Verilog — interface
- 5 connection modes of QT signal slot
- 剑指 Offer 06. 从尾到头打印链表
- Is it safe to open a stock account online?
- What is the safest app for stock account opening? Tell me what you know
- Getting started with lambda8 new features
- AB string interchange
- Single user mode
- Common dynamic memory errors
- Kali SSH Remote Login
猜你喜欢

QT excel table read / write library - qtxlsx

Dynamic memory allocation

Qcodeeditor - QT based code editor

MySQL field truncation principle and source code analysis

Afterword of Parl intensive learning 7-day punch in camp

Yolov4 coco pre train Darknet weight file

Design and implementation of thread pool

Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language

Source code analysis of zeromq lockless queue

Summary of four parameter adjustment methods for machine learning
随机推荐
Arthas source code learning-1
Detailed summary of reasons why alertmanager fails to send alarm messages at specified intervals / irregularly
Summary of four parameter adjustment methods for machine learning
Summary of common methods of ArrayList, LinkedList and vector, and analysis of source code learning
Afterword of Parl intensive learning 7-day punch in camp
Is it safe to open a stock account through the account opening link of the account manager?
JMeter reading and writing excel requires jxl jar
Using Visual Studio
Two advanced playing methods of QT signal and slot
The difference between sizeof and strlen
About?: Notes for
[C language] implementation of magic square array (the most complete)
Breakpad usage and DMP analysis
Generic - learning notes
Pytorch distributed test pit summary
JS capture, target, bubble phase
Several solutions to the distributed lock problem in partial Internet companies
Qcodeeditor - QT based code editor
0703 interface automation - MySQL database connection, encapsulation, adding database verification in use cases
Es data synchronization mode