[LeetCode] Word Break
2015-09-12 18:01
302 查看
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
这道题还是挺有难度的。重点是dynamic programming的应用。用boolean[]来判断true/false与否。记得除了contians()的条件外还要判断两个被分开的单词是否连在一起这样的问题。
代码如下。~
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
这道题还是挺有难度的。重点是dynamic programming的应用。用boolean[]来判断true/false与否。记得除了contians()的条件外还要判断两个被分开的单词是否连在一起这样的问题。
代码如下。~
public class Solution { public boolean wordBreak(String s, Set<String> wordDict) { if(s==null&&s.length()==0){ return false; } int len=s.length(); boolean[] test=new boolean[len]; for(int i=0;i<len;i++){ for(int j=0;j<=i;j++){ String sub=s.substring(j,i+1); if(wordDict.contains(sub)&&(j==0||test[j-1])){ test[i]=true; break; } } } return test[len-1]; } }
相关文章推荐
- 阿里云配置
- 在线重定义
- java_集合01
- 使用Echarts总结之——使用柱状图和地图与后台数据交互
- WebView控件
- 登陆页面的搭建
- 经济--比特币
- rails学习笔记:维护商品信息任务A
- JNIEXPORT 报错:此声明没有存储类或类型说明符
- ISO/IEC 9899:2011 条款6.4.5——字符串字面量
- COCI2014 KLOPKA
- PHP Simple HTML DOM解析器
- Android 获取手机内存存储容量信息
- 【leetocde】179. Largest number
- HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】
- bzoj 1211
- VS2015编译VLC2.2.1(under WIN7-64)
- bzoj 1211
- rpm包管理和yum命令的介绍
- php之无限极分类