class Solution { public boolean wordBreak(String s, List<String> wordDict) { Set<String> wordDictSet=new HashSet<>(wordDict); //定义一个集合 boolean[] dp=new boolean[s.length()+1]; //定义一个动态规划数组 dp[0]=true; for(int i=1;i<=s.length();i++){ //定义n位动态数组 for(int j=0;j<i;j++){ if(dp[j]&&wordDictSet.contains(s.substring(j,i))){ dp[i]=true; break; } } } return dp[s.length()]; } }