微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Leetcode 问题 472,为什么 DFS 不再起作用使用 Memo?

如何解决Leetcode 问题 472,为什么 DFS 不再起作用使用 Memo?

我想知道我是否可以在这里问 leetcode 问题,但我在这里发布了我的问题。希望有人能提供慷慨的帮助! XD!

对于这个问题: Leetcode 472,连接词:https://leetcode.com/problems/concatenated-words/

谁能回答为什么 DFS 不再工作(带备忘录),带备忘录的 DFS 有时间限制例外? Trie 解决方案似乎工作得很好。

谁能帮我分析一下这两个大O?这是怎么发生的?

谢谢!

DFS 解决方案(曾经有效):

class Solution {
    HashMap<String,Boolean> map = new HashMap<>();
    private boolean check (String s,Set<String> words,int min) {
        if (map.containsKey(s)) {
            return map.get(s);
        }
        for (int i = min; i <= s.length() - min; i++) {
            String second = s.substring(i);
            if (words.contains(s.substring(0,i)) && (words.contains(second) || check (second,words,min))) {
                map.put(s,true);
                return true;
            }
        }
        return false;
    }
    public List<String> findAllConcatenatedWordsInADict(String[] words) {
        List<String> list = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        Set<String> set = new HashSet<>();
        for (String s : words) {
            if (s.length() == 0) {
                continue;
            }
            set.add(s);
            min = Math.min(min,s.length());
        }
        for (String s : set) {
            if (check (s,set,min)) {
                list.add(s);
            }
        }

        return list;
    }
}

尝试解决方案(从其他人提交的代码中找到):

    class Solution {
TrieNode root;
public List<String> findAllConcatenatedWordsInADict(String[] words) {
    Arrays.sort(words,new Comparator<String>() {
       public int compare(String a,String b){
           return a.length() - b.length();
       } 
    });

    List<String> res = new ArrayList<>();
    this.root = new TrieNode();

    for (String word : words) {
        if (search(word,0)) {
            res.add(word);
            continue;
        }
        insert(word);
    }

    return res;
}

class TrieNode {
    boolean isLeaf;
    TrieNode[] children;
    public TrieNode() {
        isLeaf = false;
        children = new TrieNode[26];
    }
}

private void insert(String word) {
    TrieNode curr = root;
    for (int i = 0; i < word.length(); i++) {
        int idx = word.charat(i) - 'a';
        if (curr.children[idx] == null) {
            curr.children[idx] = new TrieNode();
        }
        curr = curr.children[idx];
    }
    curr.isLeaf = true;
}

// dfs
private boolean search(String word,int start,int count) {
    if (start == word.length()) {
        return count >= 2;
    }
    TrieNode curr = root;
    for (int pos = start; pos < word.length(); pos++) {
        int index = word.charat(pos) - 'a';
        if (curr.children[index] != null) {
            if(curr.children[index].isLeaf) {
                // find one word,like cat of catdog,record pos of catdag
                // go to root to search later part of catdog,like dog
                if (search(word,pos + 1,count + 1)) {
                    return true;
                }
            }
            curr = curr.children[index];
        } else {
            // this branch is invalid for word,terminate earlier 
            if (count == 0) break;
            return false;
        }
    }


    return false;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。