leetcode——139.单词拆分

这道题的动态规划,我自己没有想清楚,看了答案之后才恍然大悟,不难。

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

 

 

——2020.6.27

posted @ 2020-06-27 21:45  欣姐姐  阅读(101)  评论(0编辑  收藏  举报