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

C++

class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
vector<bool> flag(s.length()+1,false);
flag[0] = true;
for(int i = 1; i < s.length() + 1; ++i){
for (int j = i + 1; j >= 0; j--){
if (flag[j] && dict.count(s.substr(j,i-j)) != 0){
flag[i] = true;
break;
}
}
}
return flag[s.length()];
}
};

word-break leetcoder C++的更多相关文章

  1. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

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

  3. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  4. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  5. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  6. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  7. Leetcode#139 Word Break

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

  8. 【leetcode】Word Break (middle)

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

  9. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  10. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

随机推荐

  1. Shell系列(10)- bash环境变量(3)

    环境变量与用户自定义变量的区别 环境变量是全局变量,用户自定义变量是局部变量. 用户自定义变量只在当前的 shell 中生效,环境变量在当前 shell 和这个 shell 的所有子 shell 中生 ...

  2. vue 学习资料

    自学资料地址: https://zhuanlan.zhihu.com/p/26535530项目UI部分1.pc站 UI:(1)考虑自己写成本高,需要花费不少时间,好处是可以自己控制维护!(2)引入第三 ...

  3. requests接口自动化-assert断言

    断言,自动判断接口返回的结果与预期结果是否一致 from common.get_mysql import * def test_assert(): a=0 b=1 # assert a,'断言失败打印 ...

  4. 使用jemeter构造各种变量数据

    使用手动创建测试数据太麻烦,因此考虑用jmeter写了一些创建测试数据的脚本,针对那些变量非固定的数据可以利用函数来实现 通过函数助手添加各种变量数据 Tools--->函数助手 1:生成当前时 ...

  5. setTimeout 与setInterval的区别

    setTimeout(code,millisec) 方法用于在指定的毫秒数后调用函数或计算表达式 setInterval(code,millisec) 方法可按照指定的周期(以毫秒计)来调用函数或计算 ...

  6. NetCore5实现https请求

    前言 本文主要介绍在NetCore5中,实现证书加载和https访问请求. 证书准备 首先我们先创建一个自定义的证书Kiba518.pfx. 证书创建参考:最通俗易懂的RSA加密解密指导. 然后将证书 ...

  7. P4983-忘情【wqs二分,斜率优化】

    正题 题目链接:https://www.luogu.com.cn/problem/P4983 题目大意 给出长度为\(n\)的序列\(x\),记平均数为\(\bar{x}\),要求将序列分成\(m\) ...

  8. 突破GD渲染的图片马

    <?php /* The algorithm of injecting the payload into the JPG image, which will keep unchanged aft ...

  9. v-for为什么最好(一定)要加key

    v-for 指令基于一个数组来渲染一个列表,如下 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> ...

  10. Google Chrome打开权限设置开关(摄像头,录音等)

    在搜索框输入以下字符 chrome://flags/#unsafely-treat-insecure-origin-as-secure