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. 在SQL中用正则表达式替换html标签

    由于数据库的一个表字段中多包含html标签,现在需要修改数据库的字段把html标签都替换掉.当然我可以通过写一个程序去修改,那毕竟有点麻烦.直接在查询分析器中执行,但是MS SQL Server并没有 ...

  2. java 全角字符半角字符转换

    /// <summary> /// 判断字符是否英文半角字符或标点 /// </summary> /// <remarks> /// 32    空格 /// 33 ...

  3. hadoop1.X安装

    1.      配置主机的名称 master,slave1,slave2 2.      安装JDK: 3.      配置IP与主机名称的映射: 192.168.0.100 master 192.1 ...

  4. 一个Nodejs的简单计算測试程序

    測试目的: 1 測试二维数组的使用 2 输出函数的使用 代码: var util = require('util'); a = 3; b = 4; c = a + b; a = []; for(i = ...

  5. Mysql大小写敏感的问题 --转

    一.1 CREATE TABLE NAME(name VARCHAR(10)); 对这个表,缺省情况下,下面两个查询的结果是一样的: SELECT * FROM TABLE NAME WHERE na ...

  6. java中说明书/开发文档如何编写?

    由于在java开发时我们得到的或者给别人的文件一般都是class文件,不会给出源文件,故编写一个简洁易懂的说明书是必须的. ps: @param int[] arr 会有警告,可以删掉 int []. ...

  7. DiskLruCache 硬盘缓存 使用简介

    简介 LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时.对此,Google又提供了一套硬盘缓存的解决方案:DiskLru ...

  8. hdu 1728

    //hdu 1728 //这个是一道很经典的迷宫题了,思路感觉...取起点和终点,判断连线是否超过n个弯, //先是从起点出发,上下左右四个方向搜索,找到一条路,把那条路的第一个点压入队列 //然后沿 ...

  9. AjaxManager的实现

    在NeralJS思路整理一章中我们提到过实用ajax管理模块控制ajax请求,以减少ajax请求数量,优化加载,以下是ajax模块的实现,我已经确保经历简单. /** * Created by wil ...

  10. (转)DevExpress GridView属性设置

    GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据源: List<Student> list = new List<Studen ...