Word Break
来源:互联网 发布:envi处理landsat8数据 编辑:程序博客网 时间:2024/05/29 01:52
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"
.
dfs,时间复杂度O(2^n),超时。
public class Solution { public boolean wordBreak(String s, Set<String> dict) { return dfs(0,s,dict); } public boolean dfs(int start,String s, Set<String> dict){ if(start==s.length()){ return true; } boolean res=false; for(int i=start;i<s.length();i++){ if(dict.contains( s.substring(start,i+1) )){ res|=dfs(i+1,s,dict); if(res) break; } } return res; }}dp
public class Solution { public boolean wordBreak(String s, Set<String> dict) { return dp(s,dict); } public boolean dp(String s, Set<String> dict){ if(s==null || s.length()==0) return true; int n=s.length(); boolean []f=new boolean[n+1]; f[0]=true; for(int i=1;i<=n;i++){ for(int j=i-1;j>=0;j--){ if( f[j] && dict.contains( s.substring(j,i) )){ f[i]=true; break; } } } return f[n]; }}
0 0
- Word-break:break-word
- Word Break && Word Break ||
- word-break
- Word Break
- Word Break
- Word Break
- word break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- Word Break
- MySQL数据库事务隔离级别
- Python开发教程推荐
- 【九度OJ】1007【计算名次】
- metasploit(MSF)终端命令大全
- 最近遇到的好几个创业团队和创业题材
- Word Break
- 动态库头文件包含系统的库的问题
- 《STL源码剖析》---stl_hash_map.h阅读笔记
- Android自定义图文混合滚动控件(基于WheelView修改)
- iOS多线程的初步研究(二)-- 锁
- oracle sql 按时间统计(天、周、月、季、年)
- python 不超过20行的小练习
- class 的static成员变量和 static 成员函数
- 近来写一个三维家居软件的历程