国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 212. Word Search II

Flands / 3088人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

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

相關(guān)文章

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

    摘要:復(fù)雜度時(shí)間空間為長(zhǎng)度,為大小空間復(fù)雜度是是因?yàn)槲矣么嫘畔ⅲ粍?dòng)態(tài)地存當(dāng)前的路徑如果用來(lái)存信息的話空間復(fù)雜度就是時(shí)間復(fù)雜度對(duì)每個(gè)點(diǎn)都要作為起始點(diǎn),對(duì)于每個(gè)起始點(diǎn),拓展一次有四個(gè)可能性四個(gè)鄰居,要拓展次長(zhǎng)度為。思路暴力搜索帶走。 Word Search I Given a 2D board and a word, find if the word exists in the grid. ...

    LuDongWei 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

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

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

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

    tain335 評(píng)論0 收藏0
  • [Leetcode] Word Search 單詞搜索

    摘要:我們可以先用待查單詞建立一個(gè)字典樹(shù),這樣我們?cè)趶木仃囍心硞€(gè)點(diǎn)開(kāi)始深度優(yōu)先搜索時(shí),可以直接用字典樹(shù)判斷當(dāng)前組成的字符串是否是某個(gè)單詞的前綴。字典樹(shù)同樣也提供接口,所以同樣可以用于判斷是否已經(jīng)搜索到這個(gè)詞了。 Word Search I 更新的思路與解法請(qǐng)?jiān)L問(wèn):https://yanjia.me/zh/2018/11/... Given a 2D board and a word, f...

    objc94 評(píng)論0 收藏0
  • leetcode部分題目答案之JavaScript版

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

    alphahans 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<