0
点赞
收藏
分享

微信扫一扫

【c#】单词拆分

晒大太阳了 2022-04-13 阅读 59
动态规划

public class Solution {

    public bool WordBreak(string s, IList<string> wordDict) {

        var wordDictSet = new HashSet<string>(wordDict);//c#哈希表的定义

      

        var dp=new bool[s.Length+1];

        dp[0]=true;

        for(int i=1;i<=s.Length;i++){

            for(int j=0;j<i;j++){

                if(dp[j]&&wordDictSet.Contains(s.Substring(j, i - j))) {

                    dp[i]=true;

                    break;

                }

            }

           

        }

         return dp[s.Length];

    }

}

举报

相关推荐

0 条评论