美文网首页
139. Word Break

139. Word Break

作者: 夜皇雪 | 来源:发表于2016-12-16 05:09 被阅读0次

接着说dic可能很大怎么办,建立字典树,要会写

public class Solution {
    public boolean wordBreak(String s, Set<String> wordDict) {
        boolean[] res=new boolean[s.length()+1];
        res[0]=true;
        for(int i=1;i<=s.length();i++){
            for(int j=0;j<i;j++){
                if(res[j]&&wordDict.contains(s.substring(j,i))){
                    res[i]=true;
                    break;
                }
            }
        }
        return res[s.length()];
    }
}

相关文章

网友评论

      本文标题:139. Word Break

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