LeetCode:Word Break(DP)
题目地址:http://oj.leetcode.com/problems/word-break/
简单的动态规划问题,采用自顶向下的备忘录方法,代码如下:
class Solution {
public:
bool dictContain(unordered_set<string> &dict, string s)
{
unordered_set<string>::iterator ite = dict.find(s);
if(ite != dict.end())
return true;
else return false;
}
bool wordBreak(string s, unordered_set<string> &dict)
{
// Note: The Solution object is instantiated only once and is reused by each test case.
if(dict.empty())
return false;
const int len = s.size();
bool canBreak[len]; //canBreak[i] = true 表示s[0~i]是否能break
memset(canBreak, , sizeof(bool)*len);
for(int i = ; i <= len; i++)
{
if(canBreak[i-] == false && dictContain(dict, s.substr(, i)))
canBreak[i-] = true;
if(canBreak[i-] == true)
{
if(i == len)return true;
for(int j = ; j <= len - i; j++)
{
if(canBreak[j+i-] == false && dictContain(dict,s.substr(i, j)))
canBreak[j+i-] = true;
if(j == len - i && canBreak[j+i-] == true)return true;
}
}
}
return false;
}
};
注意:在本机调试时,编译器要开启c++11支持,因为#include<unordered_set>是c++11的标准
相关参考资料
微信公共账号“待字闺中”也有关于此题的讨论:请点击 这里 或 这里
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3384853.html
LeetCode:Word Break(DP)的更多相关文章
- Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game)
Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game) 爱丽丝和鲍勃一起玩游戏,他们轮流行动.爱丽丝先手开局. 最初,黑板上有一个数字 N .在每个玩家的回合,玩家需 ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)
Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
随机推荐
- 局域网内搭建git
git简介:请大家参看git官网的介绍 http://git-scm.com/book/zh/v1 还有这位大神的git教程:http://www.liaoxuefeng.com/wiki/0013 ...
- 传递给后台的Json数据解析
后台代码如下: public void ProcessRequest(HttpContext context) { context.Response.ContentType = "appli ...
- 常用oracle函数
一.逗号拼接字段 SELECT LISTAGG(aa, ',') WITHIN GROUP (ORDER BY aa) AS AA FROM *** where id<5 输出结果例如:1,2, ...
- 一个完整的WSDL文档及各标签详解
<?xml version="1.0" encoding="UTF8" ?> <wsdl:definitions targetNamespac ...
- cocos2d-x之使用plist文件初试
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } FileUtils *fu=FileUtils::getInstan ...
- hyperv 创建第二代虚拟机
环境:宿主机windows 8.1,虚拟机:windows 8.1 硬件:笔记本电脑,无线网络,没有有线网络网络配置先不设置 1.安装hyperv,控制面版-->程序和功能-->启用或关闭 ...
- Linux 安装 redis
环境:centos7 参考:http://blog.csdn.net/lk10207160511/article/details/50364088 步骤如下: 安装redis: 打开终端 输入 s ...
- 虚拟机Linux----Ubuntu1204----设置固定Ip
1.介绍 环境:ubuntu版本是12.04,虚拟机是Oracle Vm VirtualBox 2.说明 需求:现在已经安装了一个ubuntu系统,网络配置是默认选择桥接,可以上网,物理机可以连接虚拟 ...
- 如何切入 Linux 内核源代码
Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天就是逃课,上网,玩游戏,睡觉.毕业的时候,人家跟我说Makefile我完全不知,但是一说Make Love我就来劲了 ...
- [转]Android输出Log到文件
前言:开发中遇到mx4这款机型Eclipse联调不上,logcat看不了,需要输出生成文件查看调试信息.网上搜了下,功能很完善了.startService和过滤输出信息需要自己添加设置,另外注意添加权 ...