当前位置:网站首页>Sword finger offer 04 Find in 2D array
Sword finger offer 04 Find in 2D array
2022-06-25 15:36:00 【anieoo】
Original link : The finger of the sword Offer 04. Search in a two-dimensional array

solution:
Violent simulation , Time complexity 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;
}
};Time complexity of binary search line by line 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;
}
};Start at the top right , If 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;
}
};
边栏推荐
- Differences and solutions of redis cache avalanche, cache penetration and cache breakdown
- Could not connect to redis at 127.0.0.1:6379 in Windows
- Esp8266 building smart home system
- [paper notes] contextual transformer networks for visual recognition
- Is it safe to open a stock account through the account opening link of the account manager?
- Is it safe to open a stock account through the account opening link given by the account manager? I want to open an account
- CPU over high diagnosis and troubleshooting
- Data preprocessing - normalization and standardization
- Statistical analysis - data level description of descriptive statistics
- Using reentrantlock and synchronized to implement blocking queue
猜你喜欢

Shared memory synchronous encapsulation

Agent and classloader

剑指 Offer II 091. 粉刷房子

Image segmentation based on deep learning: network structure design

MySQL performance optimization - index optimization

Paddlepaddle paper reproduction course biggan learning experience

Download and installation tutorial of consumer

User defined data type - structure

Esp8266 building smart home system

剑指 Offer 04. 二维数组中的查找
随机推荐
Go build reports an error missing go sum entry for module providing package ... to add:
Statistical analysis - data level description of descriptive statistics
Completabilefuture of asynchronous tools for concurrent programming
Go language template text/template error unexpected EOF
CV pre training model set
Es data synchronization mode
How to package rpm
Joseph Ring - formula method (recursive formula)
The difference between sizeof and strlen
Weka download and installation
Reflection - learning notes
Basic syntax and common commands of R language
Differences and solutions of redis cache avalanche, cache penetration and cache breakdown
Fishing detection software
Globally unique key generation strategy - implementation principle of the sender
Solve the go project compilation error go mod: no such file or directory
Client development (electron) data store
A deformation problem of Hanoi Tower
Boost listening port server
(2) Relational database