成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 212. Word Search II

Flands / 3137人閱讀

Problem

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Example:

Input: 
words = ["oath","pea","eat","rain"] and board =
[
  ["o","a","a","n"],
  ["e","t","a","e"],
  ["i","h","k","r"],
  ["i","f","l","v"]
]

Output: ["eat","oath"]

Note:
You may assume that all inputs are consist of lowercase letters a-z.

Solution
class Solution {
    class TrieNode {
        TrieNode[] children = new TrieNode[26];
        String word;
    }
    
    private TrieNode buildTrie(String[] words) {
        TrieNode root = new TrieNode();
        for (String word: words) {
            TrieNode cur = root;
            for (char ch: word.toCharArray()) {
                int i = ch-"a";
                if (cur.children[i] == null) cur.children[i] = new TrieNode();
                cur = cur.children[i];
            }
            cur.word = word;
        }
        return root;
    }
    
    public List findWords(char[][] board, String[] words) {
        List res = new ArrayList<>();
        TrieNode root = buildTrie(words);
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                dfs(board, i, j, root, res);
            }
        }
        return res;
    }
    
    private void dfs(char[][] board, int i, int j, TrieNode root, List res) {
        char ch = board[i][j];
        if (ch == "#" || root.children[ch-"a"] == null) return;
        
        root = root.children[ch-"a"];
        if (root.word != null) { //found one
            res.add(root.word);
            root.word = null; //de-dup
        }
        
        board[i][j] = "#";
        if (i > 0) dfs(board, i-1, j, root, res);
        if (j > 0) dfs(board, i, j-1, root, res);
        if (i < board.length-1) dfs(board, i+1, j, root, res);
        if (j < board[0].length-1) dfs(board, i, j+1, root, res);
        board[i][j] = ch;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/72136.html

相關(guān)文章

  • [Leetcode] Word Search I&II 二維字符矩陣查找單詞

    摘要:復(fù)雜度時間空間為長度,為大小空間復(fù)雜度是是因為我用存信息,只動態(tài)地存當(dāng)前的路徑如果用來存信息的話空間復(fù)雜度就是時間復(fù)雜度對每個點都要作為起始點,對于每個起始點,拓展一次有四個可能性四個鄰居,要拓展次長度為。思路暴力搜索帶走。 Word Search I Given a 2D board and a word, find if the word exists in the grid. ...

    LuDongWei 評論0 收藏0
  • 前端 | 每天一個 LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...

    張漢慶 評論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡單難度攻略已經(jīng)到題,所以后面會調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時,攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...

    tain335 評論0 收藏0
  • [Leetcode] Word Search 單詞搜索

    摘要:我們可以先用待查單詞建立一個字典樹,這樣我們在從矩陣中某個點開始深度優(yōu)先搜索時,可以直接用字典樹判斷當(dāng)前組成的字符串是否是某個單詞的前綴。字典樹同樣也提供接口,所以同樣可以用于判斷是否已經(jīng)搜索到這個詞了。 Word Search I 更新的思路與解法請訪問:https://yanjia.me/zh/2018/11/... Given a 2D board and a word, f...

    objc94 評論0 收藏0
  • leetcode部分題目答案之JavaScript版

    摘要:自己沒事刷的一些的題目,若有更好的解法,希望能夠一起探討項目地址 自己沒事刷的一些LeetCode的題目,若有更好的解法,希望能夠一起探討 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 評論0 收藏0

發(fā)表評論

0條評論

Flands

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<