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 ...
随机推荐
- PHP 静态缓存
今天来说说PHP页面的静态缓存. 根据个人理解,由于客户端重复的请求某个页面,导致该页面短时间内被重复请求相同的数据,导致给服务端一定的压力,同时用户访问速度也会变慢.此时如果把这个页面缓存起来,客户 ...
- Python操作 Memcache、Redis、RabbitMQ
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- dom4j配置文件
# 我们必须实例化 ConsoleAppender # 01.实例化对象 ConsoleAppender console =new ConsoleAppender(); # log4j.appende ...
- 记录一次参加D2前端技术论坛的杭州之行
前言 在这里,闰土首先要感谢以下两位大佬提供的门票,分别是来自新浪微博部门的H同学,以及来自小米科技的D同学. 当我周六晚上在青旅写完这篇文章过后,第二天上网发现,已经有大佬提前一步在掘金上发布了高质 ...
- 约瑟夫问题 小孩报数问题poj3750
小孩报数问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15228 Accepted: 6778 Descripti ...
- kafak集群安装-转
前言 最近在利用Spark streaming和Kafka构建一个实时的数据分析系统,对图书阅读数据进行分析,做实时推荐.Spark Streaming 模块是对于 Spark Core 的一个扩展, ...
- LintCode-买卖股票的最佳时机
如果有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多仅仅同意完毕一次交易(比如,一次买卖股票),设计一个算法来找出最大利润. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 ...
- Java 并发专题 : Executor具体介绍 打造基于Executor的Webserver
转载标明出处:http://blog.csdn.net/lmj623565791/article/details/26938985 继续并发,貌似并发的文章非常少有人看啊~哈~ 今天准备具体介绍jav ...
- 负载均衡之基于L7负载
L7负载平衡 还有一种较为经常使用的负载平衡解决方式则是L7负载平衡.顾名思义,其主要通过OSI模型中的第七层应用层中的数据决定怎样分发负载. 在执行时.L7负载平衡server上的操作系统会将接收到 ...
- File attachment or query results size exceeds allowable value of 1000000 bytes.
今天早晨,收到了作业执行失败的邮件(前几天还能正常执行该作业.不知为何今天出错) 邮件显示,作业的第三个步骤报错. step3内容: msdb.dbo.sp_send_dbmail @prof ...