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

记得最開始做动态规划的题时是打开过这道题的,可是那时没什么思路。如今动态规划的题刷了不少了,今天再做这道题时非常快就想出了状态和状态转移方程。不能不说还是有点进步。

定义A[i]表示0到下标为i的子字符是否能被切割成dict中的多个单词。
那么A[i]与A[j],0<=j< i都有关系,即A[i]与前A[]中的前i-1项都有关系,详细为:

  1. 假设A[0]为1。推断s中下标从1開始到i结束的字符是否在dict中,假设在,设置A[i]为1,跳出。否则进入第二步。
  2. 假设A[1]为1,推断s中下标从2開始到i结束的字符是否在dict中。假设在。设置A[i]为1,跳出,否则进入第二步;
    …..
    这样一直遍历到A[i-1]位置。

    在上面的遍历过程中假设遍历到某一步j,A[j]=1而且j+1到i表示的字符串出如今dict中,表示前j个字符串能切割成dict中的单词,j+1到i中的字符串串也能切割成dict中的单词。这样表示前i个字符能被切割成dict中的单词。

实际编写代码时,j能够从i開始倒着開始遍历,这样能够降低遍历的次数。

runtime:4ms

class Solution {
public:bool wordBreak(string s, unordered_set<string>& wordDict) {int length=s.size();int *A=new int[length]();for(int i=0;i<length;i++){for(int j=i;j>=0;j--){if(j==i){A[i]=isExist(s,0,i,wordDict);}else if(A[j]==1){A[i]=isExist(s,j+1,i,wordDict);}if(A[i]==1)break;}}return A[length-1]==1;}int isExist(string &s,int first,int last,unordered_set<string> &wordDict){string str=s.substr(first,last-first+1);if(wordDict.count(str))return 1;elsereturn 0;}};

转载于:https://www.cnblogs.com/zsychanpin/p/7197159.html

LeetCode139:Word Break相关推荐

  1. C++word break断字(基于单词或字符串)(附完整源码)

    C++word break断字的实现 C++word break断字的完整源码(定义,实现,main函数测试) C++word break断字的完整源码(定义,实现,main函数测试) #includ ...

  2. leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)

    题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...

  3. LeetCode——Word Break

    LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...

  4. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  5. 【记忆化递归+DP】LeetCode 139. Word Break

    LeetCode 139. Word Break Solution1: 记忆化递归的典型套路题 参考网址:https://zxi.mytechroad.com/blog/leetcode/leetco ...

  6. LeetCode Word Break II

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

  7. Word Break Word Break II

    leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...

  8. Leetcode word break I II 词句拆分I和II的java实现及解析

    Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...

  9. 【Leetcode】139. 拆分词句(Word Break)

    Leetcode - 139 Word Break (Medium) 题目描述:给定一个字符串 s 与字典 wordDict,判断 s 是否能拆分成 wordDict 中的子字符串. Input: s ...

最新文章

  1. java基础编程题(2)
  2. 全了!从Python入门到入魔
  3. goroutine 那些事
  4. 二十一、Python爬取百度文库word文档内容
  5. 32 | 答疑(四):阻塞、非阻塞 I/O 与同步、异步 I/O 的区别和联系
  6. cocos2d 解密ccbi_怎么让cocosbuilder制作的动画(cocos2d-x的ccbi动画)播放完删除自己?...
  7. 零售业有效利用物联网的几种方法
  8. 高光谱数据集_文献选读|从地面和空间高光谱数据中提取红边位置参数,以估算水稻冠层叶氮含量...
  9. laravel created_at 时间戳_Laravel 单行为控制器设计的魅力
  10. Android中CursorLoader的使用、原理及注意事项
  11. FFMPEG结构体分析:AVFrame
  12. UVA 11044
  13. 解决“应用程序无法启动,因为应用程序的并行配置不正确“问题
  14. CAD工程图纸转jpg格式教程
  15. 贵州等保测评机构工程师(DJCP)目录-贵州等级保护测评机构工程师名单
  16. imagemagick 图片合并
  17. mysql insert 1062_mysql insert error 1062
  18. Linux环境非root用户配置SSH免密登录(配置原理)
  19. 利用报废主板制作SPD刷内存编程器座子
  20. 使用 Packer 自动执行 VMware vsphere 模板

热门文章

  1. 数据库查询前十条数据_西门子PLC1200组态王-Access数据库-⑨数据库查询
  2. left join和left outer join的区别
  3. 综合布线中所需要的的带宽和数据速率
  4. 7个HTML你可能不知道的使用技巧
  5. 微软最强命令行工具 Windows Terminal,强势霸榜GitHub
  6. 45道CSS基础面试题
  7. 关于WCF、WebAPI、WCFREST、WebService之间的区别总结
  8. app上线发布流程_APP上线发布流程
  9. 实时操作系统与通用计算机操作系统的区别,实时操作系统(RTOS)和通用操作系统(OS)之间的区别...
  10. 碎片化学前端,促进技术提升,我推荐这些