当前位置:网站首页>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;
}
};
边栏推荐
- Incluxdb2 buckets create database
- [opengl] face pinching system
- Yolov5进阶之七目标追踪最新环境搭建(二)
- High quality workplace human beings must use software to recommend, and you certainly don't know the last one
- Yolov5 advanced nine target tracking example 1
- Byte practice plane longitude 2
- Zzuli:1054 monkeys eat peaches
- PS tips - draw green earth with a brush
- CentOS7部署哨兵Redis(带架构图,清晰易懂)
- Yolov5 advanced 8 format conversion between high and low versions
猜你喜欢
[ue4] Niagara's indirect draw
5.2-5.3
Devaxpress: range selection control rangecontrol uses
On MEM series functions of C language
C language DUP function
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
C language to realize mine sweeping
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
随机推荐
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
[opengl] bone animation blending effect
Detailed explanation of four modes of distributed transaction (Seata)
Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
7-1 positive integer a+b (15 points)
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
556. The next larger element III: simple construction simulation questions
There are links in the linked list. Can you walk three steps faster or slower
Zzuli:1049 square sum and cubic sum
[opengl] face pinching system
NOI OPENJUDGE 1.5(23)
Web server code parsing - thread pool
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
[ue4] Niagara's indirect draw
Yolov5 advanced nine target tracking example 1
[graphics] adaptive shadow map
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
Rasterization: a practical implementation (2)
牛客 BM83 字符串變形(大小寫轉換,字符串反轉,字符串替換)
Zzuli:1046 product of odd numbers