当前位置:网站首页>Sword finger offer 12 Path in matrix
Sword finger offer 12 Path in matrix
2022-06-26 21:05:00 【xzystart】
Given a m x n Two dimensional character grid board And a string word word . If word Exists in the grid , return true ; otherwise , return false .
The words must be in alphabetical order , It's made up of letters in adjacent cells , among “ adjacent ” Cells are those adjacent horizontally or vertically . Letters in the same cell are not allowed to be reused .
for example , In the following 3×4 The matrix of contains the word “ABCCED”( The letters in the word are marked ).
Example 1:
Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output :true
Example 2:
Input :board = [[“a”,“b”],[“c”,“d”]], word = “abcd”
Output :false
Tips :
1 <= board.length <= 200
1 <= board[i].length <= 200
board and word It consists of upper and lower case English letters only

class Solution {
public boolean exist(char[][] board, String word) {
// Traversing a two-dimensional array takes each element as a header dfs
char[] words = word.toCharArray();
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(helper( i, j, board, words,0)) return true; // If there is a successful match, exit
}
}
return false;
}
//dfs Traverse
boolean helper(int i,int j,char[][] board,char[] word,Integer index) {
// Termination conditions
if(i >= board.length || i < 0 || j >= board[0].length || j < 0||board[i][j] != word[index]){
return false;
} // Passed the above if It proves that the current element has been matched All subsequent operations are matched with the previous operations
if ((index == word.length - 1)){
// It matches and is the last element
return true;
}
else{
// It matches but is not the last element
// operation
board[i][j] = '\0';
// Recursively call
boolean res = helper(i+1,j,board,word,index+1)||helper(i-1,j,board,word,index+1)
||helper(i,j+1,board,word,index+1)||helper(i,j-1,board,word,index+1);
// Call... In four directions dfs
// After the recursion, the empty element will be restored , Because the complete array is required to restart the call ( The main function needs to access each element in the array as a recursive header ,
// Then you need a complete board)
// Changing to blank only means that during this search , This element has been accessed , When initial i j When the change , Another search process started
board[i][j] = word[index];
return res; // Return the result of the call ( Is there a match in these four directions )
}
}
}
Be careful : To prevent access to elements that have already been accessed , You need to empty the accessed element .
But at the end of recursion, you need to set the element back to its original value , Because you may need to switch heads recursively
边栏推荐
- VB.net类库,获取屏幕内鼠标下的颜色(进阶——3)
- Swagger: how to generate beautiful static document description pages
- 30. concatenate substrings of all words
- JWT操作工具类分享
- Flutter TextField详解
- Garbage collection mechanism of browser
- 0基础学c语言(1)
- Detailed explanation of shutter textfield
- 孙老师版本JDBC(2022年6月12日21:34:25)
- [Bayesian classification 4] Bayesian network
猜你喜欢
![[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination](/img/f9/68b5b5ce21f4f851439fa061b477c9.jpg)
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination

西瓜书重温(七): 贝叶斯分类器(手推+代码demo)

Review of watermelon book (VII): Bayesian classifier (manual push + code demo)

【protobuf 】protobuf 升级后带来的一些坑

Super VRT

Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005

Android IO, a first-line Internet manufacturer, is a collection of real questions for senior Android interviews

清华大学就光刻机发声,ASML立马加紧向中国出口光刻机

Distributed ID generation system

动态规划111
随机推荐
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
这些地区考研太疯狂!哪个地区报考人数最多?
0 basic C language (0)
基于Qt实现的“合成大西瓜”小游戏
Is it safe to open a securities account? Is there any danger
云计算技术的发展与芯片处理器的关系
【连载】说透运维监控系统01-监控系统概述
c语言99乘法表
MySQL中存储过程的详细详解
0 basic C language (2)
0基础学c语言(1)
不要做巨嬰了
JWT operation tool class sharing
Garbage collection mechanism of browser
Browser event loop
浏览器事件循环
清华大学就光刻机发声,ASML立马加紧向中国出口光刻机
【山东大学】考研初试复试资料分享
Idea error: process terminated
孙老师版本JDBC(2022年6月12日21:34:25)