LeetCode——Word Break
LeetCode——Word Break
Question
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
Solution
动态规划的思想,从头开始扫描字符串,判断当前子字符串是否可以在字典中查找到,取决于自身以及对这个子字符串的所有划分方式是否可以查找到。
递推关系式子: dp[i] = dp[j] && dp[i - j] (0 <= j <= i)
初始值 dp[0] = true; 表示子字符串长度为0的时候,是可以查找到的。
Answer
public:bool wordBreak(string s, vector<string>& wordDict) {if (wordDict.size() == 0 || s.empty())return false;vector<int> dp(s.length() + 1, false);dp[0] = true;for (int i = 1; i <= s.length(); i++) {for (int j = 0; j <= i; j++) {if (dp[j]) {// 第二个参数表示,表示从j开始的字符个数string str1 = s.substr((unsigned int)j, i - j);if (check(wordDict, str1)) {dp[i] = true;break;}}}}return dp[s.length()];}bool check(vector<string>& wordDict, string& str1) {for (string str : wordDict)if (str == str1)return true;return false;}
};
转载于:https://www.cnblogs.com/zhonghuasong/p/6957705.html
LeetCode——Word Break相关推荐
- Leetcode word break I II 词句拆分I和II的java实现及解析
Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...
- [LeetCode] Word Break II 拆分词句之二
[LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...
- 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 java,Word Break leetcode java
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)
题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...
- 【DFS + 记忆化递归】LeetCode 140. Word Break II
LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...
- 【记忆化递归+DP】LeetCode 139. Word Break
LeetCode 139. Word Break Solution1: 记忆化递归的典型套路题 参考网址:https://zxi.mytechroad.com/blog/leetcode/leetco ...
- 【Leetcode】139. 拆分词句(Word Break)
Leetcode - 139 Word Break (Medium) 题目描述:给定一个字符串 s 与字典 wordDict,判断 s 是否能拆分成 wordDict 中的子字符串. Input: s ...
最新文章
- int main(int argc, char* argv[])
- GDCM:生成标准SOP类的测试程序
- idea springBoot 配置devtools实现热部署
- EXE与SYS通信(直接访问模式)
- CentOS部署Harbor镜像仓库(1),java技术栈自我理解面试题通俗解说
- MTK 驱动开发(18)---LCD 参数理解
- WPF教程六:布局之Grid面板(转)
- iOS 中delegate的理解与使用(传值)
- 如何把自己写的python程序给别人用
- 大学用什么python教材_Python开发基础 大学教材
- 有效值(RMS) 平均值(DC) 的理解
- jsjq面试笔记(上)
- 四层七层负载均衡区别
- nyoj 191 小柯的问题
- 电脑作为sntp服务器配置
- HTML5期末大作业:旅游网页设计与实现——四川成都-(9页 带购物车)
- 考生合格证书打印的部分代码和生成证书方法
- Matlab实践课心得体会,实验心得体会4篇
- SQL Server 损坏修复
- SQL注入漏洞 详解
热门文章
- php 二维数组按照某value值求出最大值最小值
- js设计模式之Constructor(构造器)
- hdu 4608 I-number(13多校#1 ,1009)
- 【琥珀】带你用好CLIP!视觉-语言表征学习新进展
- 更深更宽的孪生网络,有效提升目标跟踪精度,代码开源
- 一图胜千言,这本交互式线代教科书让你分分钟理解复杂概念,佐治亚理工出品...
- 高达82 fps的实时文本检测,华科AAAI2020提出可微分二值化模块
- 近期计算机视觉相关算法竞赛汇总—总奖池超553万人民币
- 首次!阿里达摩院将Pure Transformer 应用于目标重识别ReID!
- 太赞了!谷歌、DeepMind提出高效Transformer评估基准