当前位置: 首页 > news >正文

【Hot100】LeetCode—79. 单词搜索

目录

  • 1- 思路
    • 回溯
  • 2- 实现
    • ⭐79. 单词搜索——题解思路
  • 3- ACM 实现


  • 原题链接:79. 单词搜索

1- 思路

回溯

思路:① 遍历每个单元格(作为起点)、②对每个单元格进行回溯(起点回溯)
1- 起点

  • 用两层 for 循环遍历每个单元格,之后对当前单元格进行 dfs

2- 回溯操作

  • 由于需要对每个单元格进行回溯,所以 ij的位置,回溯函数必须要知道,因此传参中必须有 ij

2- 实现

⭐79. 单词搜索——题解思路

在这里插入图片描述

class Solution {boolean[][] used ;public boolean exist(char[][] board, String word) {used = new boolean[board.length][board[0].length];int row = board.length;int col = board[0].length;for(int i = 0 ; i < row;i++){for(int j = 0 ; j < col;j++){if(backTracing(board,word,0,i,j)){return true;}}}return false;}int[][] dir = {{-1,0},{0,-1},{1,0},{0,1}};public boolean backTracing(char[][] board,String word,int index,int i ,int j ){// 结果收集if(word.length() == index){return true;}// 结束判断if( i < 0 || i>=board.length || j<0 || j>=board[0].length || used[i][j] || board[i][j] != word.charAt(index)){return false;}// 回溯// 标记为已使用used[i][j] = true;for(int[] d:dir){int nextX = i + d[0];int nextY = j + d[1];if(backTracing(board,word,index+1,nextX,nextY)){return true;}}used[i][j] = false;return false;}
}

3- ACM 实现

public class exist {static boolean[][] used ;public static boolean exist(char[][] board, String word) {used = new boolean[board.length][board[0].length];int row = board.length;int col = board[0].length;for(int i = 0 ; i < row;i++){for(int j = 0 ; j < col;j++){if(backTracing(board,word,0,i,j)){return true;}}}return false;}static int[][] dir = {{-1,0},{0,-1},{1,0},{0,1}};public static boolean backTracing(char[][] board,String word,int index,int i ,int j ){// 结果收集if(word.length() == index){return true;}// 结束判断if( i < 0 || i>=board.length || j<0 || j>=board[0].length || used[i][j] || board[i][j] != word.charAt(index)){return false;}// 回溯// 标记为已使用used[i][j] = true;for(int[] d:dir){int nextX = i + d[0];int nextY = j + d[1];if(backTracing(board,word,index+1,nextX,nextY)){return true;}}used[i][j] = false;return false;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String boardInput = scanner.nextLine();String word = scanner.nextLine();char[][] board = parseBoard(boardInput);boolean res = exist(board, word);System.out.println(res);}private static char[][] parseBoard(String input) {input = input.substring(1, input.length() - 1); // Remove outer bracketsString[] rows = input.split("],\\[");int rowCount = rows.length;int colCount = rows[0].split(",").length;char[][] board = new char[rowCount][colCount];for (int i = 0; i < rowCount; i++) {String[] cols = rows[i].replaceAll("[\\[\\]\"]", "").split(",");for (int j = 0; j < colCount; j++) {board[i][j] = cols[j].charAt(0);}}return board;}
}


http://www.mrgr.cn/news/14536.html

相关文章:

  • Linux查看jvm相关参数以及设置调优参数
  • MFC工控项目实例之八选择下拉菜单添加打钩图标
  • BaseCTF-Web-Week2-WP
  • linux修改文件的修改时间
  • 三天吃透Java面试八股文
  • 从0到1框架搭建,Python+Pytest+Allure+Git+Jenkins接口自动化框架(超细整理)
  • 不同搜索引擎蜘蛛的功能、‌抓取策略与技术实现差异探究
  • ArrayList 和 LinkedList 的区别?
  • Android 11.0 关于定制自适应AdaptiveIconDrawable类型的动态时钟图标的功能实现系列一
  • Redis中事务与乐观锁
  • 设计模式之建造者模式
  • 在VB.net中,LINQ有什么方法与属性
  • 代码随想录算法训练营第三十天|452. 用最少数量的箭引爆气球 435. 无重叠区间 763.划分字母区间
  • Prometheus监控Kubernetes ETCD
  • 这款SpringBoot+Vue酒店管理系统,你绝对值得拥有
  • SpringBoot集成kafka-监听器注解
  • CARLA Drone: 首个实现从不同空中视角进行单目3D目标检测,并提供数据集
  • jvm监控工具一览
  • 【No module named ‘pcapy‘】报错解决方法
  • 公网信息泄露监测(网盘、暗网、搜索引擎、文档平台)思路分享