Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255950
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-22 17:08:30

























import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
//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".




public class WordBreak {
public static void main(String[] args){

// boolean[] a=new boolean[2];
// System.out.print(a[1]);
Set  b=new HashSet();
b.add("b");
b.add("abc");
b.add("b");
b.add("cd");
System.out.print(wordBreak("abcd",b));

}
public static boolean wordBreak(String s, Set dict) {
   if (s==null)
    return true;
   if(dict.size()==0)
    return false;
       int n =s.length();
       boolean[] dp=new boolean[n+1];
       dp[0]=true;
       for(int i=1;i<=n;i++){
        if(dp[i-1]){
        int idx=i-1;
        for(int j=idx;j         String cur=s.substring(idx,j+1);
        if(dict.contains(cur)){
        dp[j+1]=true;
        }
        }
        }
       }
//        System.out.print(dp[0]);
return dp[n];
         
   }
}






import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class WordBreak2 {
  public List  wordBreak(String s, Set dict) {
     List temp=new ArrayList();
     List b=new ArrayList();
     b=firstmatch(s,dict);
//      for(int i=0;i<=)
 return temp;
       
   }
  public List firstmatch(String s, Set dict){
 String[] a=(String[])dict.toArray();
 List temp=new ArrayList();
 for(int i=0;i if(s.substring(0, a[i].length()).equals(a[i])){
temp.add(a[i]);
}
 }
 return temp;

 
  }
}
阅读(282) | 评论(0) | 转发(0) |
0

上一篇:threeSum

下一篇:LRU Cache

给主人留下些什么吧!~~