zoukankan      html  css  js  c++  java
  • leetcode212

     1 class Solution {
     2     public List<String> findWords(char[][] board, String[] words) {
     3     List<String> res = new ArrayList<>();
     4     TrieNode root = buildTrie(words);
     5     for (int i = 0; i < board.length; i++) {
     6         for (int j = 0; j < board[0].length; j++) {
     7             dfs (board, i, j, root, res);
     8         }
     9     }
    10     return res;
    11 }
    12 
    13 public void dfs(char[][] board, int i, int j, TrieNode p, List<String> res) {
    14     char c = board[i][j];
    15     if (c == '#' || p.next[c - 'a'] == null) return;
    16     p = p.next[c - 'a'];
    17     if (p.word != null) {   // found one
    18         res.add(p.word);
    19         p.word = null;     // de-duplicate
    20     }
    21 
    22     board[i][j] = '#';
    23     if (i > 0) dfs(board, i - 1, j ,p, res); 
    24     if (j > 0) dfs(board, i, j - 1, p, res);
    25     if (i < board.length - 1) dfs(board, i + 1, j, p, res); 
    26     if (j < board[0].length - 1) dfs(board, i, j + 1, p, res); 
    27     board[i][j] = c;
    28 }
    29 
    30 public TrieNode buildTrie(String[] words) {
    31     TrieNode root = new TrieNode();
    32     for (String w : words) {
    33         TrieNode p = root;
    34         for (char c : w.toCharArray()) {
    35             int i = c - 'a';
    36             if (p.next[i] == null) p.next[i] = new TrieNode();
    37             p = p.next[i];
    38        }
    39        p.word = w;
    40     }
    41     return root;
    42 }
    43 
    44 class TrieNode {
    45     TrieNode[] next = new TrieNode[26];
    46     String word;
    47 }
    48 }

    参考:https://leetcode.com/problems/word-search-ii/discuss/59780/Java-15ms-Easiest-Solution-(100.00)

  • 相关阅读:
    开发进度01
    eclipse 调用cmd运行DataX
    kettle 新建DB连接双击打不开
    用户体验评价
    找水王课堂练习
    人月神话阅读笔记01
    用户模板和用户场景
    大道至简阅读笔记03
    第九周
    第八周总结
  • 原文地址:https://www.cnblogs.com/asenyang/p/10496420.html
Copyright © 2011-2022 走看看