美文网首页程序员
力扣 140 单词拆分 II

力扣 140 单词拆分 II

作者: zhaojinhui | 来源:发表于2020-10-23 00:49 被阅读0次

题意:给定一个字符串和一个字典,返回可以用字典拼接的所有字符串

思路:用set记录字典的单词,用list array记录前i个字符串能够有多少钟组合,然后遍历字符串,在每次遍历中,再从0到i遍历子区间,如果j到i的字符串在字典中,且0到j个字符串有大于0个组合,那么跟新0到i个组合个数

思想:字符串的组合

复杂度:时间O(n3),空间O(n2)

class Solution {
    public List<String> wordBreak(String s, List<String> wordDict) {
        if(s.length() > 100){
            return new ArrayList();
        }
        Set<String> set = new HashSet(wordDict);
        List<String>[] list = new ArrayList[s.length()+1];
        List<String> temp = new ArrayList();
        temp.add("");
        list[0] = temp;
        for(int i=1;i<=s.length();i++) {
            temp = new ArrayList();
            for(int j=0;j<i;j++) {
                String cur = s.substring(j, i);
                if(set.contains(cur) && list[j].size()>0) {
                    for(String str: list[j]) {
                        String t = str + " " + cur;
                        temp.add(t.trim());
                    }
                }
            }
            list[i]=temp;
        }
        return list[s.length()];
    }
}

相关文章

  • LeetCode-140-单词拆分 II

    LeetCode-140-单词拆分 II 140. 单词拆分 II[https://leetcode-cn.com...

  • 力扣 140 单词拆分 II

    题意:给定一个字符串和一个字典,返回可以用字典拼接的所有字符串 思路:用set记录字典的单词,用list arra...

  • 140. 单词拆分 II

    140. 单词拆分 II[https://leetcode-cn.com/problems/word-break-...

  • python实现leetcode之140. 单词拆分 II

    解题思路 动态规划dp[i]表示到i为止有哪些切分方式 140. 单词拆分 II[https://leetcode...

  • LeetCode-140-单词拆分 II

    原题链接: https://leetcode-cn.com/problems/word-break-ii/[htt...

  • leetcode140 单词拆分II

    题目 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使...

  • 力扣 139 单词拆分

    题意:给定一个单词,和一个字典,判定字典中的单词能否拼接成给定的单词 思路:一维动态规划,详情见注释 思想:动态规...

  • 每日一题之单词拆分II

    题目140:单词拆分II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空...

  • LeetCode 139. 单词拆分 | Python

    139. 单词拆分 题目来源:力扣(LeetCode)https://leetcode-cn.com/proble...

  • 单词拆分 II

    ?xml version="1.0" encoding="UTF-8"? 欢迎关注本人的微信公众号AI_Engin...

网友评论

    本文标题:力扣 140 单词拆分 II

    本文链接:https://www.haomeiwen.com/subject/sjormktx.html