当前位置:网站首页>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;
}
}
边栏推荐
- A glimpse of spir-v
- Huawei converged VLAN principle and configuration
- SPIR-V初窺
- UE4 unreal engine, editor basic application, usage skills (IV)
- Redis守护进程无法停止解决方案
- 干货!通过软硬件协同设计加速稀疏神经网络
- 一圖看懂!為什麼學校教了你Coding但還是不會的原因...
- Accelerating spark data access with alluxio in kubernetes
- Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
- Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
猜你喜欢
Leetcode skimming questions_ Invert vowels in a string
500 lines of code to understand the principle of mecached cache client driver
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
[solved] how to generate a beautiful static document description page
Kubernetes stateless application expansion and contraction capacity
Basic operations of database and table ----- delete data table
Folio.ink 免费、快速、易用的图片分享工具
c#网页打开winform exe
A picture to understand! Why did the school teach you coding but still not
Redis-字符串类型
随机推荐
Code review concerns
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
Spir - V premier aperçu
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
Redis守护进程无法停止解决方案
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Loop structure of program (for loop)
D22:indeterminate equation (indefinite equation, translation + problem solution)
Superfluid_ HQ hacked analysis
Maya hollowed out modeling
1. Introduction to basic functions of power query
Leetcode skimming questions_ Invert vowels in a string
Leetcode3. Implement strstr()
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
干货!通过软硬件协同设计加速稀疏神经网络
Reasonable and sensible
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
A Cooperative Approach to Particle Swarm Optimization
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry