码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode139:Word Break

时间:2015-06-07 23:12:44      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

思路:dp[i]=dp[j]+s.contais(s.substring(j,i+1));

技术分享
 1 public class Solution {
 2     public boolean wordBreak(String s, Set<String> wordDict) {
 3         
 4         int m=s.length();
 5         boolean isSegmented[]=new boolean[m+1];
 6         isSegmented[0]=true;
 7         int start=0;
 8         for(int i=0;i<m;i++)
 9         for(int j=0;j<=i;j++){
10            isSegmented[i+1]=isSegmented[j]&&wordDict.contains(s.substring(j,i+1));
11            if(isSegmented[i+1])
12            break;
13         }
14         return isSegmented[m];
15     }
16     
17 }
View Code

 

LeetCode139:Word Break

标签:

原文地址:http://www.cnblogs.com/gonewithgt/p/4559165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!