当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Efficient pytorch: how to eliminate training bottlenecks
- Generation method and usage of coredump
- Simulating Sir disease transmission model with netlogo
- Learning notes on February 5, 2022 (C language)
- Advertising effect cluster analysis (kmeans)
- How to deal with mining process
- [C language] 32 keyword memory skills
- Js- get the mouse coordinates and follow them
- Is it safe to open a stock account through the account opening link of the account manager?
- 3. Sequential structure multiple choice questions
猜你喜欢

JMeter reading and writing excel requires jxl jar

Learning notes on February 18, 2022 (C language)

Esp8266 building smart home system

程序员 VS 黑客的思维 | 每日趣闻

Afterword of Parl intensive learning 7-day punch in camp

basic_ String mind map

Could not connect to redis at 127.0.0.1:6379 in Windows

Arthas, a sharp tool for online diagnosis - several important commands

剑指 Offer 06. 从尾到头打印链表

Data feature analysis skills - correlation test
随机推荐
Common dynamic memory errors
What is the safest app for stock account opening? Tell me what you know
[paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning
Mining procedure processing
Install Kali extension 1: (kali resolution problem)
Day01: learning notes
Business layer - upper and lower computer communication protocol
Basic syntax and common commands of R language
JSON module dictionary and string conversion
Go closure usage example
Some usage records about using pyqt5
Distributed token
Design and implementation of thread pool
通过客户经理的开户链接开股票账户安全吗?
Pytorch | how to save and load pytorch models?
Thread - learning notes
Globally unique key generation strategy - implementation principle of the sender
Go language modifies / removes multiple line breaks in strings
Distributed transaction solution
How to download and install Weka package