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".

  DP

class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int len = s.size();
if(len <) return false;
if(dict.size() == ) return false;
vector<bool> flag(len+,false);
flag[] = true;
for(int i = ; i< len+;++i){
for(int j = i-; j>=;--j)
if(flag[j]==true && dict.find(s.substr(j,i-j))!= dict.end())
{
flag[i] = true;
break;
}
}
return flag[len];
}
};

LeetCode _ Word Break的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  5. [Leetcode Week9]Word Break

    Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...

  6. 【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 ...

  7. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  8. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  9. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

随机推荐

  1. bin

    动态链接库(Dynamic Link Library或者Dynamic-link library,缩写为DLL),是微软公司在微软视窗操作系统(即Windows操作系统)中实现共享函数库概念的一种方式 ...

  2. oracle的shutdown命令有几种参数

    SHUTDOWN NORMAL:不允许新的连接.等待会话结束.等待事务结束.做一个检查点并关闭数据文件.启动时不需要实例恢复.SHUTDOWN TRANSACTIONAL:不允许新的连接.不等待会话结 ...

  3. MyBatis(5):MyBatis集成Spring事务管理(上)

    单独使用MyBatis对事务进行管理 前面MyBatis的文章有写过相关内容,这里继续写一个最简单的Demo,算是复习一下之前MyBatis的内容吧,先是建表,建立一个简单的Student表: 1 2 ...

  4. java笔记7之录入

    1  为了让程序的数据更符合开发的数据,我们就加入了键盘录入.    让程序更灵活一下.        那么,我们如何实现键盘数据的录入呢?        A:导包            格式:   ...

  5. Python自动化之Django的CSRF

    什么CSRF? CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果 某个用户已经登录到你的网站上了,那么当这个 ...

  6. 你需要知道的九大排序算法【Python实现】之插入排序

    三.插入排序 基本思想:插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2).是稳定的排序方法.插入算 ...

  7. 二分-hdu-4768-Flyer

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目意思: 有n个A.B.C,每个Ai,Bi,Ci,对于每个P=Ai+k*Ci(P<=B ...

  8. Android学习–Android app 语言切换功能

    功能: app用户根据自己的语言喜好,设置app语言.语言设置只针对本app,并在下次启动应用时保留前一次启动设置. 更新语言: public static void changeAppLanguag ...

  9. OpenStack Neutron DVR L2 Agent的初步解析 (一)

    声明: 本博客欢迎转载,但请保留原作者信息! 作者:林凯 团队:华为杭州OpenStack团队 OpenStack Juno版本号已正式公布,这是这个开源云平台的10个版本号,在Juno版的Neutr ...

  10. jpeg和gif已经影响互联网发展进程了,他们应该被历史淘汰了!!!

    本人发现.传统的图片格式已经不适应互联网时代了!!!,故本人发起定义一种新的图片格式.后缀名为 .gnet 互联网上的图片大多有这几种来源.微博上传,视频截图,网络编辑人上传等,以眼下的技术.这些图片 ...