当前位置:网站首页>79. Word search (medium string array matrix backtracking)
79. Word search (medium string array matrix backtracking)
2022-07-28 22:25:00 【Calm in the wind and rain】
79. Word search
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 .
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”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “SEE”
Output :true
Example 3:
Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCB”
Output :false
Tips :
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board and word It consists of upper and lower case English letters only
Advanced : You can use search pruning techniques to optimize solutions , Make it board Larger situations can solve problems faster ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/word-search
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
backtracking . Need to consider marking letters , And trace back to delete the tag after use , In addition, you need to pay attention to how to get the return value .
Answer key (Java)
class Solution {
public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
boolean flag = check(board, word, i, j, 0);
if (flag) {
return true;
}
}
}
return false;
}
private boolean check(char[][] board, String word, int i, int j, int k) {
if (board[i][j] != word.charAt(k)) {
return false;
} else if (k == word.length() - 1) {
return true;
}
char ch = board[i][j];
board[i][j] = '0';// Mark directly on the original array
int[][] dirs = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};// Auxiliary array moving up, down, left and right
boolean result = false;
for (int[] dir : dirs) {
int row = i + dir[0], col = j + dir[1];
if (row >= 0 && row < board.length && col >= 0 && col < board[0].length) {
if (board[row][col] != '0') {
boolean flag = check(board, word, row, col, k + 1);
if (flag) {
// flag by true Indicates that the end of the string has been reached
result = true;
break;
}
}
}
}
board[i][j] = ch;// Restore array
return result;
}
}
边栏推荐
- 腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实
- vuejs中如何实现动态路由切换及路由的缓存
- Ukrainian officials: half of Ukrainian agricultural products are exported through the Danube port
- 普源示波器实际的使用效果怎么样
- CDN working principle
- Principle of object. Prototype. ToString. Call()
- [machine learning] naive Bayesian classification of text -- Classification of people's names and countries
- Establishment of Ruiji takeout development environment
- 软考网络工程师
- [LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
猜你喜欢
HCIP(11)
Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022
局域网添加DNS服务器进行域名解析
静态路由和缺省路由实验
Can the MySQL create statement be used to create a table structure and append new records
HCIP(8)
IFLYTEK written examination
hcip实验(14)
Learning notes and summary of C language programming specification
Alibaba cloud CDN practice
随机推荐
Static route and default route experiment
Can the MySQL create statement be used to create a table structure and append new records
SQL注入 Less34(POST型宽字节注入+布尔盲注)
表单验证和级联下拉列表(多种实现)
[cloud native kubernetes] mapping external service under kubernetes cluster eendpoint
What are the main functions and uses of LCR tester
CDN working principle
Why is 0.1 + 0.2 not equal to 0.3? How to solve this problem?
40. Combined sum II
From Web3 to web2.5, is it backward or another way?
普源示波器实际的使用效果怎么样
mysql create语句能不能用来建立表结构并追加新的记录
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
array_diff_assoc 元素是数组时不比较数组值的办法
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
[leetcode] maximum depth of binary tree
HCIP(8)
HCIP(12)
使用百度EasyDL实现明厨亮灶厨师帽识别
Mysql内置函数