当前位置:网站首页>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;
}
};
边栏推荐
- CPU over high diagnosis and troubleshooting
- Websocket (WS) cluster solution
- 剑指 Offer 09. 用两个栈实现队列
- Is it safe to open a stock account in Guoxin golden sun?
- Arthas source code learning-1
- Cross compilation correlation of curl Library
- golang reverse a slice
- Netlogo learning
- CV pre training model set
- [paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning
猜你喜欢
[paper notes] overview of case segmentation
Differences and solutions of redis cache avalanche, cache penetration and cache breakdown
Pytorch distributed test pit summary
Data feature analysis skills - correlation test
Data preprocessing - normalization and standardization
[C language] implementation of magic square array (the most complete)
Custom structure type
[paper notes] rethinking and improving relative position encoding for vision transformer
5 connection modes of QT signal slot
CV pre training model set
随机推荐
[C language] implementation of magic square array (the most complete)
Talk about the creation process of JVM objects
Summary of four parameter adjustment methods for machine learning
Leetcode123 timing of buying and selling stocks III
Es data synchronization mode
Finally, we can figure out whether the binding event in the tag is bracketed or not
Business layer - upper and lower computer communication protocol
JMeter reading and writing excel requires jxl jar
1090.Phone List
到底要不要去外包公司?这篇带你全面了解外包那些坑!
Afterword of Parl intensive learning 7-day punch in camp
Is it safe to open an account for new bonds? What preparations are needed
Efficient pytorch: how to eliminate training bottlenecks
Markdown learning
免费送书啦!火遍全网的AI给老照片上色,这里有一份详细教程!
Dynamic memory allocation
If a thread overflows heap memory or stack memory, will other threads continue to work
What is the safest app for stock account opening? Tell me what you know
About%*s and%* s
Websocket (WS) cluster solution