zoukankan      html  css  js  c++  java
  • 212. Word Search II

    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.

    For example,
    Given words = ["oath","pea","eat","rain"] and board =

    [
      ['o','a','a','n'],
      ['e','t','a','e'],
      ['i','h','k','r'],
      ['i','f','l','v']
    ]
    
    Return ["eat","oath"].

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


    对比第79题,79题是判断一个字符串,而212题是判断多个字符串。思路和79题类似,对矩阵里的每个元素做dfs,只不过79题判断遍历到目前的substring是否在一个字符串里,而这题判断遍历到目前的substring是否在TrieTree里(相当于对所有字符串同时检查,而不是逐个检查)。
    如果给的多个字符串有很多有相同的前缀,这个方法能降低时间复杂度。

    好慢啊:15% 如何优化!

    public class Solution {
        public class TrieNode {
            int count;
            TrieNode[] children = new TrieNode[26];
            public void insert(String word) {
                TrieNode cur = this;
                int i = 0;
                while (i < word.length()) {
                    if (cur.children[word.charAt(i) - 'a'] == null) {
                        cur.children[word.charAt(i) - 'a'] = new TrieNode();
                    }
                    cur = cur.children[word.charAt(i) - 'a'];
                    i++;
                }
                cur.count = 1;
            }
            // 0: not prefix, 1: prefix, 2: contains string
            public int startsWith(String prefix) {
                TrieNode cur = this;
                int i = 0;
                while (i < prefix.length()) {
                    if (cur.children[prefix.charAt(i) - 'a'] == null) {
                        return 0;
                    }
                    cur = cur.children[prefix.charAt(i) - 'a'];
                    i++;
                }
                if (cur.count == 0) {
                    return 1;
                } else {
                    cur.count = 0;
                    return 2;
                }
            }
        }
        TrieNode root = new TrieNode();
        public List<String> findWords(char[][] board, String[] words) {
            for (int i = 0; i < words.length; i++) {
                root.insert(words[i]);
            }
            boolean[][] visited = new boolean[board.length][board[0].length];
            List<String> re = new LinkedList<String>();
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    find(board, visited, i, j, "", re);
                }
            }
            return re;
        }
        public void find(char[][] board, boolean[][] visited, int i, int j, String str, List<String> list) {
            if (i < 0 || i > board.length - 1 || j < 0 || j > board[0].length - 1 || visited[i][j]) {
                return;
            }
            String tmp = str + board[i][j];
            int starts = root.startsWith(tmp);
            if (starts == 0) {
                return;
            } else {
                if (starts == 2) {
                    list.add(tmp);
                }
                visited[i][j] = true;
                find(board, visited, i - 1, j, tmp, list);
                find(board, visited, i + 1, j, tmp, list);
                find(board, visited, i, j - 1, tmp, list);
                find(board, visited, i, j + 1, tmp, list);
                visited[i][j] = false;
            }
        }
    }
    
  • 相关阅读:
    一个iOS图片选择器的DEMO(实现图片添加,宫格排列,图片长按删除,以及图片替换等功能)
    [小细节,大BUG]记录一些小问题引起的大BUG(长期更新....)
    利用runTime,实现以模型为主的字典转模型(注意与KVC的区别)
    项目总结(三)--- 关于版本控制器
    项目总结(一)--- 关于用到的设计模式
    EntityFramework与TransactionScope事务和并发控制
    Entity Framework与ADO.NET批量插入数据性能测试
    Mathtype公式位置偏上
    FreeSWITCH 加载模块过程解读
    FreeSWITCH调用第三方TTS 使用tts_commandline
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/7223491.html
Copyright © 2011-2022 走看看