当前位置:网站首页>Sword finger offer 12 Path in matrix
Sword finger offer 12 Path in matrix
2022-07-06 01:39:00 【Yake1965】
The finger of the sword Offer 12. Path in matrix
class Solution {
int m, n, k;
char[][] board;
char[] w;
int[][] dirs = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
public boolean exist(char[][] board, String word) {
m = board.length;
n = board[0].length;
k = word.length();
this.board = board;
w = word.toCharArray();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == w[0]) {
board[i][j] = '!';
if(f(i, j, 1)) return true;
board[i][j] = w[0];
}
}
}
return false;
}
boolean f(int i, int j, int idx){
if(idx == k) return true;
for(int[] dir : dirs){
int x = i + dir[0];
int y = j + dir[1];
if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == w[idx]){
board[x][y] = '!';
if(f(x, y, idx + 1)) return true;
board[x][y] = w[idx];
}
}
return false;
}
}
边栏推荐
- 【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
- 【全網最全】 |MySQL EXPLAIN 完全解讀
- Leetcode 208. Implement trie (prefix tree)
- Basic operations of database and table ----- set the fields of the table to be automatically added
- Threedposetracker project resolution
- Initialize MySQL database when docker container starts
- Idea sets the default line break for global newly created files
- Basic operations of databases and tables ----- default constraints
- Mongodb problem set
- Superfluid_ HQ hacked analysis
猜你喜欢

TrueType字体文件提取关键信息

普通人下场全球贸易,新一轮结构性机会浮出水面
![[flask] official tutorial -part1: project layout, application settings, definition and database access](/img/c3/04422e4c6c1247169999dd86b74c05.png)
[flask] official tutorial -part1: project layout, application settings, definition and database access

You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin

Mathematical modeling learning from scratch (2): Tools

NiO related knowledge (II)

干货!通过软硬件协同设计加速稀疏神经网络

Electrical data | IEEE118 (including wind and solar energy)

Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...

Leetcode skimming questions_ Invert vowels in a string
随机推荐
Code review concerns
Maya hollowed out modeling
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
Unity VR resource flash surface in scene
Luo Gu P1170 Bugs Bunny and Hunter
竞赛题 2022-6-26
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
NumPy 数组索引 切片
Basic operations of databases and tables ----- non empty constraints
A glimpse of spir-v
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
D22:indeterminate equation (indefinite equation, translation + problem solution)
Reasonable and sensible
Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
Mongodb problem set
普通人下场全球贸易,新一轮结构性机会浮出水面
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
02.Go语言开发环境配置
False breakthroughs in the trend of London Silver
Docker compose configures MySQL and realizes remote connection