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

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/13200048.html