139. Word Break
2016-03-22 10:26
399 查看
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 =
dict =
Return true because
Subscribe to see which companies asked this question
从头开始一截一截找……
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
Subscribe to see which companies asked this question
从头开始一截一截找……
public class Solution { private boolean search (String s,List<String> dic){ if(s.length()==0)return true; for(int i = 0;i<dic.size();i++){ int sign = 0; String tem = dic.get(i); if(tem.length()>s.length())continue; for(int j = 0;j<tem.length();j++){ if(tem.charAt(j)==s.charAt(j))continue; sign = 1;break; } if(sign==1)continue; String ss = s.substring(tem.length()); if(search(ss,dic)==true)return true; } return false; } public boolean wordBreak(String s, Set<String> wordDict) { List<String> dic = new ArrayList(wordDict);//先把不可能的字段删了 for(int i = 0;i<dic.size();){ if(s.indexOf(dic.get(i))!=-1){i++;continue;} dic.remove(i); } for(int i = dic.size()-1;i>=0;i--){//如果dict中有字符串能用其他字符串表示,就把该字符串删掉 String tem = dic.get(i); dic.remove(i); if(search(tem,dic)==true)continue; dic.add(tem); } return search(s,dic); } }
相关文章推荐
- 3月份Web服务器份额:Microsoft亚军 紧追Apache
- 69. Sqrt(x)
- iOS开发之真机获取NSLog的日志文件
- PHP通用分页组件
- linux mplayer 播放yuv格式 (转载)
- jQuery实现当拉动滚动条到底部加载数据
- Java 四种线程池
- 如何用tomcat发布自己的Java项目
- Android WebView的Js对象注入漏洞解决方案
- 在应用角度Docker容器技术带来的改变
- swift学习资源
- 简单的毫秒级C语言耗时打印宏
- nodejs aes加解密128算法,base64编码
- Linux下配置jdk1.7
- 手机GUI自动化测试介绍 | 有道技术沙龙博客
- ACdream OJ 1153 (k-GCD)
- 让你的WizFi250适应各种气候
- js如何给当前日期+1?
- Hibernate exercise 54
- HTML 表单(form) 使用详解