LeetCode:Word Break
2016-06-08 22:57
387 查看
Word Break
Total Accepted: 89221 TotalSubmissions: 347438 Difficulty: Medium
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".
Subscribe to see which companies asked this question
java code:
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for(int i=1;i<=s.length();i++) {
for(int j=0;j<i;j++) {
if(dp[j] && wordDict.contains(s.substring(j,i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}
相关文章推荐
- 斐波那契数数列
- 关于fork函数
- Linux 目录体系
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- android 弹出底部dialog
- 排序算法——直接插入法排序
- 著名ERP厂商的SSO单点登录解决方案介绍一
- Arduino舵机控制
- Arduino舵机控制
- haproxy实现discuz论坛的动静分离和负载均衡 推荐
- scala学习手记8 - 自适应的默认做法
- 使用SoundTouch开源库开发android手机上的音乐bpm检测应用
- 第十五周项目—程序补充阅读(1)
- STL中vector的实现
- Xamarin.Forms之MessagingCenter
- LINUX下QT FOR ARM开发环境搭建过程 (使用qt-x11-opensource-src-4.5.2.tar.gz进行编译)
- Java Static Block总结
- ARDUSUB 浏览
- 希尔排序代码实现
- Python pip install