当前位置:网站首页>Sword finger offer 12 Path in matrix
Sword finger offer 12 Path in matrix
2022-06-22 02:49:00 【SS_ zico】
Power button 79
Please design a function , Used to determine whether there is a path containing all characters of a string in a matrix . The path can start from any lattice in the matrix , Each step can be left in the matrix 、 Right 、 On 、 Move down one space . If a path passes through a lattice of a matrix , Then the path cannot enter the grid again . for example , In the following 3×4 The matrix of contains a string “bfce” The path of ( The letters in the path are bold ).
[[“a”,“b”,“c”,“e”],
[“s”,“f”,“c”,“s”],
[“a”,“d”,“e”,“e”]]
But the matrix doesn't contain strings “abfb” The path of , Because the first character of the string b After occupying the first row and the second lattice in the matrix , The path can't go into this grid again .
Example 1:
Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output :true
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
rows = board.size();
cols = board[0].size();
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(dfs(board, word, i, j, 0)) return true;
}
}
return false;
}
private:
int rows, cols;
bool dfs(vector<vector<char>>& board, string word, int i, int j, int k) {
if(i >= rows || i < 0 || j >= cols || j < 0 || board[i][j] != word[k]) return false;
if(k == word.size() - 1) return true;
board[i][j] = '\0';
bool res = dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i - 1, j, k + 1) ||
dfs(board, word, i, j + 1, k + 1) || dfs(board, word, i , j - 1, k + 1);
board[i][j] = word[k];
return res;
}
};
Time complexity O(MN)
Spatial complexity O(K)
边栏推荐
- Day18qt signal and slot 2021-10-29
- 2022钎焊考试模拟100题及答案
- 最新發布:Neo4j 圖數據科學 GDS 2.0 和 AuraDS GA
- GraphAcademy 课程讲解:《Neo4j 图数据科学简介》
- GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
- Kubernetes code generator use
- Use of day19qpushbutton 2021-10-30
- 关于PMP考试,你想知道的知识都在这里了
- Using hook based on xposed framework
- Ioerror: no translation files found for default language zh cn Solutions for
猜你喜欢
![[1. quick sort]](/img/3d/66ce309761d0d79a5d09718a67def8.png)
[1. quick sort]

EMC Radiation Emission rectification - principle Case Analysis

In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened

MySQL recursively finds the tree structure. This method is very practical!

How to select the appropriate version of neo4j (version 2022)

如何选择合适的 Neo4j 版本(2022版)

【7. 高精度除法】

Using neo4j sandbox to learn neo4j graph data science GDS

Two dot vertical progress styles

PMP备考相关敏捷知识
随机推荐
ATM机模拟系统
智翔金泰冲刺科创板:年营收3919万亏损超3亿 拟募资40亿
理想L9正式发布:8月底前开始交付 零售价45.98万元
Which Amazon evaluation system is better?
June25,2022 PMP Exam clearance manual-4
The neo4j skill tree was officially released to help you easily master the neo4j map database
With the acceleration of industry wide digital transformation, what kind of storage will be more popular?
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
Anaconda historical version download
Asemi fast recovery diode FR107 parameter, FR107 real object, FR107 application
并查集dsu
Two dot vertical progress styles
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
Automated tools - monitoring file changes
360EDR刨析
June25,2022 PMP Exam clearance manual-5
In the era of industrial Internet, there is no real center
fatal error: png++/png.hpp: 没有那个文件或目录
How to use tensorboard add_ histogram
Day21qt mouse event 2021-11-01