当前位置:网站首页>剑指 Offer 12. 矩阵中的路径
剑指 Offer 12. 矩阵中的路径
2022-06-21 16:53:00 【SS_zico】
力扣 79
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。
[[“a”,“b”,“c”,“e”],
[“s”,“f”,“c”,“s”],
[“a”,“d”,“e”,“e”]]
但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。
示例 1:
输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
输出: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;
}
};
时间复杂度 O(MN)
空间复杂度 O(K)
边栏推荐
- Under the banana whose R & D accounts for only 3%, is it sunscreen black technology or IQ tax in summer?
- RT-Thread 柿饼派M7 全志F133 ddr 运行xboot
- 论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》
- Node的url模块
- POSIX创建终止线程
- What is the S3 protocol that we are talking about every day? This article takes you to understand the story behind S3
- Use of list
- aws elastic beanstalk入门之简介
- Runmaide medical passed the listing hearing: it is expected that the loss will increase, with huoyunfei brothers holding about 33%
- 原码、补码、反码的关系
猜你喜欢

编写第一个C#应用程序——Hello, C#

研发仅占3%的蕉下,是防晒黑科技,还是夏天的智商税?

What is the S3 protocol that we are talking about every day? This article takes you to understand the story behind S3

STM32F1与STM32CubeIDE编程实例-线性霍尔效应传感器驱动

天天在都在谈的S3协议到底是什么?一文带你了解S3背后的故事

原码、补码、反码的关系

Operation of simulation test platform for test questions bank of R1 quick opening pressure vessel operation certificate in 2022

Redis6.0 new features (Part 1)

数字经济王宁教你如何正确地选择短期投资

Byte Jump propose un nouveau type de réseau léger et efficace, mobovit, qui surpasse GhostNet et mobilenetv3 dans la classification, la détection et d'autres tâches CV!
随机推荐
我被变相降薪了
Cann training camp Season 2 - the opening ceremony | it starts at 7:30 tonight on time. You can't miss it!
[technical management] assembly number and sword team
C语言与Lua的交互(实践三)
AttributeError: ‘Book‘ object has no attribute ‘sheet‘
How to create your own AI creativity?
From demand to open source, how to look at it with new eyes?
Under the banana whose R & D accounts for only 3%, is it sunscreen black technology or IQ tax in summer?
Node的json解析
Excess rlsp
字节流量生意经:变现趁早、缝钱袋子、All in卖货
Not graduated from a first-class university, but passed the self-study software test and entered the initial 22K annual salary of Alibaba
About the basic operation of xlrd Library (for beginners)
List set to comma concatenated string
EtherCAT igh function attempt
力扣461.汉明距离
Viewing technological changes through Huawei Corps (IV): interactive media (Music)
C语言与Lua的交互(实践二)
C语言dll动态链接库
科普大佬说 | 如何打造自己的AI创造力?