leetcode — word-break
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Source : https://oj.leetcode.com/problems/word-break/
*
*
* 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".
*
*/
public class WordBreak {
/**
* 判断给定的字符串是否能分割为多个单词,每个单词都包含在给定的字典中
*
* 1. 使用DFS,如果能到达字符串最后,说明可以break
* 2. 题目中只是判断是否的问题,并不需要求出具体break的方法,对于是否的问题可以使用DP来解决
*
* dp[i]:表示str[i-1]能否被break,比如dp[1]表示str[0:0]能否被break
* dp[0] = true
* dp[i] = true,当:
* 存在0 <= k <= i-1, dp[k] = true, 并且tr[k:i-1] 存在dic中
*
* @param str
* @param dic
* @return
*/
public boolean wordBreak (String str, Set<String> dic) {
boolean[] dp = new boolean[str.length()+1];
dp[0] = true;
for (int i = 0; i < str.length(); i++) {
for (int j = i; j > -1; j--) {
if (dp[j] && dic.contains(str.substring(j, i+1))) {
dp[i + 1] = true;
break;
}
}
}
return dp[str.length()];
}
public static void main(String[] args) {
WordBreak wordBreak = new WordBreak();
String[] dicStr = new String[]{"leet", "code"};
boolean result = wordBreak.wordBreak("leetcode", new HashSet<String>(Arrays.asList(dicStr)));
System.out.println(result + " == true");
}
}
leetcode — word-break的更多相关文章
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- 基于Spring Aop实现类似shiro的简单权限校验功能
在我们的web开发过程中,经常需要用到功能权限校验,验证用户是否有某个角色或者权限,目前有很多框架,如Shiro Shiro有基于自定义登录界面的版本,也有基于CAS登录的版本,目前我们的系统是基于C ...
- BigDecimal工具类处理精度计算
/** * Created by My_coder on 2017-07-27. * 加减乘除计算工具类 */ public class BigDecimalUtil { private BigDec ...
- Java-----关于eclipse导入项目发生的问题及解决办法
今天通过eclipse导入了几个项目,项目名出现红叉,对于我这样的强迫症来说是无法容忍的,故现做总结,遇到同学可按照以下方法来操作. 改动的地方主要是两个方面: 1.Tomcat版本问题. 此问题是由 ...
- hdu 4939
题意: 长度为n个单位的map,每一个单位须要时间t来走完. 每一个单位能够放置一个塔.一共同拥有三种塔,每种塔的作用不同: 1.仅仅能攻击当前单位.每秒x点伤害(红塔) 2.攻击当前单位之后的全部单 ...
- 入门Webpack
---恢复内容开始--- 什么是WebPack,为什么要使用它? 为什要使用WebPack 现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包.为了简化 ...
- Spring Boot实战之逐行释义HelloWorld
一.前言 研究Spring boot也有一小段时间了,最近会将研究东西整理一下给大家分享,大概会有10~20篇左右的博客,整个系列会以一个简单的博客系统作为基础,因为光讲理论很多东西不是特别容易理解 ...
- License友好的前端组件合集
在做Web开发过程中,不可避免的会用到各种UI组件.通常,我们并不会需要什么组件,都去自己开发的,网上有那么多好用的,我们为什么要自己造轮子呢?我通常只会在网上找不到合适的组件时,才会去自己开发一套. ...
- 本地代码上传到GitHub---拷贝github代码
来这里: 转载请标明出处: http://blog.csdn.net/hanhailong726188/article/details/46738929 步骤: git init git add na ...
- Laravel学习笔记(二)
解决了类自动加载的问题,剩下的问题就是看文档了,laravel的官方文档虽然简单,但是却包含了很多基础知识,学习Laravel最好先看看官方文档,我感觉帮助很大,因为laravel框架的源码看起来并不 ...
- ADB usb 或 WiFi 连接手机 ADB工具下载
ADB usb 或 WiFi 连接手机 ADB工具下载 小米 MIUI8.5 手机开启USB调试 1.设置-->我的手机-->全部参数-->MIUI版本--->连续点N次 2. ...