79. Word Search

这篇具有很好参考价值的文章主要介绍了79. Word Search。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

79. Word Search

import numpy as np

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        m,n=len(board),len(board[0])
        dic=defaultdict(set)
        for i in range(m):
            for j in range(n):
                dic[board[i][j]].add((i,j))

        def dfs(cord,indx):
            if indx==lngth:
                # if word_isr:results.append(word[::-1])
                # else:results.append(word)
                return True
 
            ch=word[indx]
            i,j=cord
            for cand in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
                if cand in dic[ch]:
                    dic[ch].remove(cand)
                    flag=dfs(cand,indx+1)
                    dic[ch].add(cand)
                    if flag:return True
            return False
        

        lngth=len(word)
        for cord in list(dic[word[0]]):
            dic[word[0]].remove(cord)
            flag=dfs(cord,1)
            dic[word[0]].add(cord)
            if flag:return True
        return False

how to store the location that previously visited文章来源地址https://www.toymoban.com/news/detail-765771.html

到了这里,关于79. Word Search的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【英文干货】【Word_Search】找单词游戏(第1天)

    Awareness 意识 Breathing 呼吸 Calm 平静的 De-Stress 减压 Feelings 感受,情感 Inspection 调查 Meditation 冥想 Peace 和平 Quiet 安静的 Recollection 回忆 Relaxation 放松 Retention 保留 Serenity 祥和 Sitting 坐着 Time 时间 Zen 禅

    2024年01月22日
    浏览(25)
  • 剑指 Offer 12. 矩阵中的路径 / LeetCode 79. 单词搜索(深度优先搜索)

    链接:剑指 Offer 12. 矩阵中的路径;LeetCode 79. 单词搜索 难度:中等 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相

    2024年02月02日
    浏览(29)
  • LeetCode74.Search-A-2d-Matrix<搜索二维矩阵>

    题目:   思路: 矩阵,搜索数是否在矩阵内。那就查找他是否在每一行中。如果符合这一行的范围,那就一直找这一列是否存在,如果存在返回true;否则false;  

    2024年02月16日
    浏览(33)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    Algorithms practice:leetcode33 Search in Rotated Sorted Array There is an integer array ,nums , sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated,at an unknown pivot index k (1 = k nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums

    2024年01月21日
    浏览(36)
  • leetcode分类刷题:二分查找(Binary Search)(四、基于值域的数组/矩阵类型)

    基于值域的二分法与基于定义域的题型不同,它的目标是从一“ 特殊排序序列 ”中确定“第k个元素值”,而不像基于定义域的题型是从排序序列中找小于等于特定target值的第一个索引;同时,针对“特殊排序序列”,往往需要 嵌套使用双指针 法进行操作,进一步增加了对

    2024年02月11日
    浏览(42)
  • Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)

    Capacity To Ship Packages Within D Days Medium 8.6K 179 Companies A conveyor belt has packages that must be shipped from one port to another within days days. The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maxi

    2024年02月09日
    浏览(25)
  • Leetcode 290. Word Pattern

    Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. Use the dictionary to save the two list.

    2024年02月07日
    浏览(28)
  • leetcode - 290. Word Pattern

    Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. Example 1: Example 2: Example 3: Constraints: Use hashmap to map pattern to s Time complexity: o ( n ) o(n) o ( n ) Space complexity: o ( n ) o(n) o ( n )

    2024年02月09日
    浏览(18)
  • leetcode - 527. Word Abbreviation

    Given an array of distinct strings words, return the minimal possible abbreviations for every word. The following are the rules for a string abbreviation: The initial abbreviation for each word is: the first character, then the number of characters in between, followed by the last character. If more than one word shares the same abbreviation, then perform th

    2024年01月24日
    浏览(27)
  • LeetCode //C - 290. Word Pattern

    Given a pattern and a string s , find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s .   Example 1: Input: pattern = “abba”, s = “dog cat cat dog” Output: true Example 2: Input: pattern = “abba”, s = “dog cat cat fish” Output: false Example 3

    2024年02月13日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包