• 139. 单词拆分


        public boolean wordBreak(String s, List<String> wordDict) {
            return wordBreakSet(s, new HashSet<>(wordDict));
        }
    
        private boolean wordBreakSet(String s, HashSet<String> wordDict) {
            if (s.isEmpty()) {
                return true;
            }
            for (int i = 0; i < s.length(); i++) {
                String before = s.substring(0, i);
                String after = s.substring(i);
                if (wordDict.contains(after) && wordBreakSet(before, wordDict)) {
                    return true;
                }
            }
            return false;
        }
    
    • 递归+记忆 accept,时间复杂度:O(N^2), 空间复杂度O(N):递归的深度
    public boolean wordBreak(String s, List<String> wordDict) {
            return wordBreakSet(s, new HashSet<>(wordDict), new HashSet<>());
        }
    
        private boolean wordBreakSet(String s, HashSet<String> wordDict, HashSet<String> memory) {
            if (s.isEmpty()) {
                return true;
            }
            if (memory.contains(s)) return false; //记忆
            for (int i = 0; i < s.length(); i++) {
                String before = s.substring(0, i);
                String after = s.substring(i);
                if (wordDict.contains(after) && wordBreakSet(before, wordDict, memory)) {
                    return true;
                }
            }
            memory.add(s); //记忆
            return false;
        }
    
    • 单纯的BFS,超时
    public boolean wordBreak(String s, List<String> wordDict) {
            LinkedList<String> queue = new LinkedList<>();
            queue.add(s);
            while (!queue.isEmpty()) {
                String seg = queue.poll();
                for (String word : wordDict) {
                    if (seg.startsWith(word)) {
                        String otherSeg = seg.substring(word.length());
                        if (otherSeg.isEmpty()) {
                            return true;
                        }
                        queue.add(otherSeg);
                    }
                }
            }
            return false;
        }
    
    • BFS + 记忆,accept
    public boolean wordBreak(String s, List<String> wordDict) {
            LinkedList<String> queue = new LinkedList<>();
            queue.add(s);
            Set<String> memory = new HashSet<>(); //记忆
            while (!queue.isEmpty()) {
                String seg = queue.poll();
                if (!memory.contains(seg)) { //记忆
                    for (String word : wordDict) {
                        if (seg.startsWith(word)) {
                            String otherSeg = seg.substring(word.length());
                            if (otherSeg.isEmpty()) {
                                return true;
                            }
                            queue.add(otherSeg);
                        }
                    }
                    memory.add(seg); //记忆
                }
            }
            return false;
        }
    
    • dp 动态规划
    public boolean wordBreak(String s, List<String> wordDict) {
            boolean[] dp = new boolean[s.length()];//dp[i]代表字符串s截止到下标i时的子字符串是否可拆分,即 s.substring(0,i) 是否可拆分。
            dp[0] = true;
            Set<String> set = new HashSet<>(wordDict);
            for (int i = 0; i < s.length(); i++) {
                for (int j = i; j >= 0; j--) {
                    boolean before = dp[j];
                    String middle = s.substring(j, i);
                    String after = s.substring(i);
                    if (before && (middle.isEmpty() || set.contains(middle)) && set.contains(after)) {
                        return true;
                    }
                    if (before && set.contains(middle)) {
                        dp[i] = true;
                        break;
                    }
                }
            }
            return false;
        }
    
    • dp 动态规划(代码优化)
    public boolean wordBreak(String s, List<String> wordDict) {
            boolean[] dp = new boolean[s.length() + 1];
            dp[0] = true;
            Set<String> set = new HashSet<>(wordDict);
            for (int i = 0; i <= s.length(); i++) { //注意边界条件
                for (int j = 0; j < i; j++) {
                    if (dp[j] && set.contains(s.substring(j, i))) {
                        dp[i] = true;
                        break;
                    }
                }
            }
            return dp[s.length()];
        }
    
  • 相关阅读:
    ZLL网关程序分析
    ZLL主机接口的信息处理流程
    TI Zigbee Light Link 参考设计
    基于能量收集的智能家居-2013国家级大学生创业实践项目申报_商业计划书_V0.2
    office excel 装Visual Studio后报错解决方案
    php随机生成验证码
    Mysql添加外键约束
    hdu 1232 畅通工程
    hdu 1162 Eddy's picture (Kruskal 算法)
    hdu 1102 Constructing Roads (Prim算法)
  • 原文地址:https://www.cnblogs.com/lasclocker/p/11410026.html
Copyright © 2020-2023  润新知