码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【LeetCode】No.79. Word Search -- Java Version


    题目链接:https://leetcode.com/problems/word-search/

    1. 题目介绍(Word Search)

    Given an m x n grid of characters board and a string word, return true if word exists in the grid.

    【Translate】: 给定一个m × n的字符网格board和一个字符串word,如果单词在网格中存在,则返回true。

    The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

    【Translate】: 这个单词可以由顺序相邻的单元格的字母构成,其中相邻的单元格水平或垂直相邻。同一个字母单元格不能使用多次。

    【测试用例】:
    testcase1
    testcase2
    testcase3

    【条件约束】:
    constraints

    【跟踪】:

    follow up
    【Translate】: 你能否使用剪枝搜索来使你的解决方案在一个更大的board中运行的更快?

    2. 题解

    2.1 递归 – O(3 * word.length * m * n)

    原题解来自于 JacksonOan 的 Java, Simple with Explanation!

    该题解的核心思路就是先找到符合条件的第一个字符,然后以此向上下左右去进行回溯,通过设置边界条件来判断对错。该题还可以扩展成,判断是否有多个符合条件的结果,如果有,有几个?

    此外这里的代码与原题解代码略有不同的是去掉了b[r][c] == '0' ,这是因为在正确的回溯过程中,b[r][c]还没有被恢复,仍然处被改变的状态,所有在递归的过程中它就根本不可能出现==word.chatAt(start)的那个值。

    class Solution {
        private static int m,n;
        public boolean exist(char[][] board, String word) {
            m = board.length;
            n = board[0].length;
            
            for (int i = 0; i < m; i++){
                for (int j = 0; j < n; j++){
                    if (board[i][j] == word.charAt(0) && search(board,word,0,i,j)) return true;
                }
            }
            return false;
        }
        
        public boolean search(char[][] b, String w, int index, int i, int j){
            if (i < 0 || i >= m || j < 0 || j >= n || b[i][j] != w.charAt(index)) return false;
            
            if (index == w.length()-1) return true;
            
            char tmp = b[i][j];
            b[i][j] = '0';
            
            if (search(b,w,index+1,i+1,j) ||
               search(b,w,index+1,i-1,j) ||
               search(b,w,index+1,i,j+1) ||
               search(b,w,index+1,i,j-1)) return true;
            
            b[i][j] = tmp;
            return false;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    act1

    3. 参考资料

    [1] Prune-and-Search | A Complexity Analysis Overview | GeeksforGeeks

  • 相关阅读:
    Java单例模式的完整实现,从0到1带你一步步优化
    ElasticStack日志分析平台-ES 集群、Kibana与Kafka
    笔试强训day35(抄送列表,年会抽奖)
    计算机网络 - 应用层 选择复习题
    使用PyTorch搭建VGG模型进行图像风格迁移实战(附源码和数据集)
    C# ManualResetEvent 类分析
    【蓝桥杯真题练习】STEMA科技素养练习题库 答案版010 持续更新中~
    商机来了,又一电商巨头即将诞生!
    arp 隔离
    思维导图软件 ConceptDraw MINDMAP mac中文特色介绍
  • 原文地址:https://blog.csdn.net/qq_41071754/article/details/127690511
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号