LeetCode139:Word Break
2015-06-07 21:40
351 查看
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
思路:dp[i]=dp[j]+s.contais(s.substring(j,i+1));
View Code
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));
public class Solution { public boolean wordBreak(String s, Set<String> wordDict) { int m=s.length(); boolean isSegmented[]=new boolean[m+1]; isSegmented[0]=true; int start=0; for(int i=0;i<m;i++) for(int j=0;j<=i;j++){ isSegmented[i+1]=isSegmented[j]&&wordDict.contains(s.substring(j,i+1)); if(isSegmented[i+1]) break; } return isSegmented[m]; } }
View Code
相关文章推荐
- 过长的函数---要重构的信号
- 连接mysql问题 mysqlnd cannot connect to MySQL 4.1+ using old authentication
- 读书笔记之java编程思想2
- 那个地方,那些刻骨铭心(上)
- Linux运维 第三阶段 (六)web相关概念httpd服务及https加密传输配置
- IOS开发-提升app性能的25条建议和技巧
- 初识机房收费系统
- 递归实现全排列(一)
- java中间件学习1-java中间件的定义
- hdu 2056
- 初学Linux 命令
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication解决办法
- LeetCode --- Add Binary
- 移动平台WEB前端开发技巧汇总
- ios UI学前须知
- iOS开发网络—03HTTP协议
- 设计类图
- 小Q系列故事――为什么时光不能倒流
- 初识Linux-C/C++开发环境
- Remove Duplicates from Sorted Array