leetcode--Word Break
来源:互联网 发布:安东尼戴维斯数据 编辑:程序博客网 时间:2024/06/16 08:22
Given a string s and a dictionary of words dict, determine ifs 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"
.
题意:给定一个字符串s,和字典,判断s能否由字典内的一个或者多个单词组合而成。
分类:动态规划
解法1:动态规划。使用flag[i]来表示从0到i构成的子串能否用字典来表示,那么我们要求的就是flag[s.length()]
对于flag[i],它由flag[j]和s.substring(j,i)这个子串决定,如果i之前存在一个j,使flag[j]为true,那么我们只要判断j到i这部分子串,是否在字典里面
如果在,那么显然flag[i]也为true
public class Solution { public boolean wordBreak(String s, Set<String> wordDict) {if(s.length()==0) return true;if(wordDict.isEmpty()) return false;boolean flag[] = new boolean[s.length()+1];flag[0] = true;for(int i=1;i<=s.length();i++){for(int j=i-1;j>=0;j--){if(flag[j]&&wordDict.contains(s.substring(j,i))){//判断子串flag[i] = true;break;}}}return flag[s.length()]; }}
0 0
- leetcode Word Break & Word Break ||
- Leetcode: Word Break
- [leetcode]Word Break
- [leetcode]Word Break II
- LeetCode:Word Break
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break
- [LeetCode] Word Break II
- LeetCode: Word Break
- leetcode之Word Break
- [LeetCode]Word Break II
- [LeetCode]Word Break
- leetcode-Word Break
- LeetCode 之 Word Break
- leetcode word break
- LeetCode | Word Break
- LeetCode | Word Break II
- mysql查询语句的不等于写法
- afnetwork如何实现同步请求
- 雨松MOMO UGUI 之 全面理解图集与使用(三)
- 【趋势科技实习录】 PIT testing with OSCE11
- 虚拟继承与虚函数继承的内存布局
- leetcode--Word Break
- netty启动
- 3. Laravel5学习笔记:laravel队列使用教程
- Spring事务配置的五种方式
- 泛型的基本使用
- php魔术方法的使用(总结)
- ffmpeg和SDL的多媒体编程(一)制作屏幕录像
- 对监听静态注册配置的理解
- android平台的jni