当前位置:网站首页>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;
}
};边栏推荐
- 4-29——4.32
- C string format (decimal point retention / decimal conversion, etc.)
- Zzuli:1053 sine function
- PHP GD image upload bypass
- [ue4] material and shader permutation
- C language memory function
- Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
- Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
- [graphics] real shading in Unreal Engine 4
- Talking about part of data storage in C language
猜你喜欢

PS tips - draw green earth with a brush

Remote server background hangs nohup

Bucket sorting in C language

Byte practice surface longitude

C string format (decimal point retention / decimal conversion, etc.)

To improve efficiency or increase costs, how should developers understand pair programming?

ASTC texture compression (adaptive scalable texture compression)

Série yolov5 (i) - - netron, un outil de visualisation de réseau
![[opengl] advanced chapter of texture - principle of flowmap](/img/dd/6208122fcc578caaf098301b185e03.jpg)
[opengl] advanced chapter of texture - principle of flowmap

Vs+qt multithreading implementation -- run and movetothread
随机推荐
My QT learning path -- how qdatetimeedit is empty
Zzuli:1046 product of odd numbers
Série yolov5 (i) - - netron, un outil de visualisation de réseau
Web server code parsing - thread pool
NOI OPENJUDGE 1.6(09)
Write a 2-minute countdown.
7-10 stack of hats (25 points) (C language solution)
零拷贝底层剖析
Introduction to opengl4.0 tutorial computing shaders
Dllexport et dllimport
[opengl] bone animation blending effect
[ue4] Niagara's indirect draw
NOI OPENJUDGE 1.5(23)
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
C language to implement a password manager (under update)
Yolov5 advanced seven target tracking latest environment construction (II)
On MEM series functions of C language
mmdetection 学习率与batch_size关系
Global and Chinese markets for transparent OLED displays 2022-2028: Research Report on technology, participants, trends, market size and share
Talking about part of data storage in C language