leetcode(word-break)
来源:互联网 发布:linux mongodb安装 编辑:程序博客网 时间:2024/05/22 15: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”.
解题思路:使用动态规划算法。首先找到前面能够直接作为单词的子串并将它赋值为true。
public boolean wordBreak(String s, Set<String> dict) { boolean word[]=new boolean[s.length()];//保存前面i长度的字符串能否划分的值 int i; for(i=0;i<s.length();i++) if(dict.contains(s.substring(0, i+1)))word[i]=true; else word[i]=false; for(i=0;i<s.length();i++){ for(int j=0;j<=i;j++){ if(word[j]&&dict.contains(s.substring(j+1,i+1))) {//如果前j个字符能够进行划分而且j到i的子串是一个单词则说明前j个字符能够划分 word[i]=true; break; } } } return word[s.length()-1]; }
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
- Unity开发PC端游戏遇到的坑<一>
- variable handling 函数 empty— 检查一个变量是否为空
- 理解 Objective-C Runtime
- 管理者
- sql分组后最大一条记录
- leetcode(word-break)
- 线程的使用
- 随机生成18位密码
- Java 7之基础 - 强引用、弱引用、软引用、虚引用
- 常用Git命令
- C++ 学习基础篇(一)—— C++与C 的区别
- 17、RedHat5 LB集群(LVS)-NAT模式
- javascript中闭包的工作原理
- Pods 更新后提示Bundle资源找不到