139. Word Break

2016-12-16  本文已影响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()];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读