当前位置:网站首页>Search in the two-dimensional array of leetcode sword offer (10)
Search in the two-dimensional array of leetcode sword offer (10)
2022-07-03 14:59:00 【& eternal Galaxy &】
Title Description
In a n * m In a two-dimensional array , Each row is sorted in ascending order from left to right , Each column is sorted in ascending order from top to bottom . Please complete an efficient function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Example :
The existing matrix matrix as follows :
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
Coding ideas
The idea of depth first traversal of graph , You can also use the width of the graph to traverse the idea first , You can do it yourself .
Python3 Code implementation
class Solution:
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
def isvalid(matrix, i, j):
# Control boundaries
if len(matrix) == 0:
return False
h = len(matrix) - 1
w = len(matrix[0]) - 1
if i >=0 and i <= h and j >=0 and j <= w:
return True
return False
def isVisited(visited, i, j):
# Have you been visited before
if not visited[i][j]:
visited[i][j] = True
return True
return False
def dfs(matrix, target, visited,i, j):
# Depth first traversal implementation
if isvalid(matrix, i, j) and isVisited(visited, i, j):
if matrix[i][j] == target:
return True
if matrix[i][j] > target:
return dfs(matrix, target, visited, i, j-1) or dfs(matrix, target, visited, i-1, j)
if matrix[i][j] < target:
return dfs(matrix, target, visited, i, j+1) or dfs(matrix, target, visited, i+1, j)
return False
if len(matrix) == 0:
return False
visited = [[False] * len(matrix[0]) for _ in range(len(matrix))]
return dfs(matrix, target, visited, 0, 0)C++ Code implementation
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
if(matrix.size() == 0)
return false;
vector<vector<bool>> visited(matrix.size(), vector<bool>(matrix[0].size(), false));
return dfs(matrix, visited, target, 0, 0);
}
public:
bool isValid(vector<vector<int>>& matrix, int i, int j){
if(matrix.size() == 0)
return false;
int h = matrix.size() - 1;
int w = matrix[0].size() - 1;
if(i>=0 && i<=h && j>=0 && j<=w)
return true;
return false;
}
public:
bool isVisited(vector<vector<bool>>& visited, int i, int j){
if(!visited[i][j]){
visited[i][j] = true;
return true;
}
return false;
}
public:
bool dfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int target, int i, int j){
if(isValid(matrix, i, j) && isVisited(visited, i, j)){
if(matrix[i][j] == target)
return true;
if(matrix[i][j] > target)
return dfs(matrix, visited, target, i, j-1) || dfs(matrix, visited, target, i-1, j);
if(matrix[i][j] < target)
return dfs(matrix, visited, target, i, j+1) || dfs(matrix, visited, target, i+1, j);
}
return false;
}
};边栏推荐
- How does vs+qt set the software version copyright, obtain the software version and display the version number?
- Awvs batch operation script
- My QT learning path -- how qdatetimeedit is empty
- Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
- NOI OPENJUDGE 1.5(23)
- Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
- There are links in the linked list. Can you walk three steps faster or slower
- [ue4] cascading shadow CSM
- B2020 分糖果
- C language memory function
猜你喜欢

Vs+qt application development, set software icon icon

Tencent internship interview sorting
![[wechat applet] wxss template style](/img/28/f9d12bf761e25f9564d92697cf049d.png)
[wechat applet] wxss template style

Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th

【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》

QT program font becomes larger on computers with different resolutions, overflowing controls
![[graphics] efficient target deformation animation based on OpenGL es 3.0](/img/53/852ac569c930bc419846ac209c8d47.jpg)
[graphics] efficient target deformation animation based on OpenGL es 3.0

Unity hierarchical bounding box AABB tree

Remote server background hangs nohup

Yolov5系列(一)——网络可视化工具netron
随机推荐
Piwigo 2.7.1 sqli learning
Zzuli: sum of 1051 square roots
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
PS tips - draw green earth with a brush
Tensor 省略号(三个点)切片
Rasterization: a practical implementation (2)
[graphics] efficient target deformation animation based on OpenGL es 3.0
Yolov5系列(一)——網絡可視化工具netron
Implement Gobang with C language
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Zzuli:1057 prime number determination
Zero copy underlying analysis
406. 根据身高重建队列
Zzuli:1040 sum of sequence 1
C language STR function
7-9 one way in, two ways out (25 points)
C language to implement a password manager (under update)