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

解题思路:

可以使用动态规划的思想,如果S[0~i]在dict中是可分的,那么只需考察S[i+1~n]在dict中可分即可;

具体步骤:

新建一个和S字符串长度相等的数组,用来记录S从0~i的各个子串是否被dict可分;

从S串的第一个字符开始,逐渐增加字符,考察是否可分:

对于待考察的S[0~i]字符串,如果S[0~j]已经在之前的考察中证实是可分的,那么只需判断S[j+1~i]是否可分;

代码:

 class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
vector<bool> seg_flag(s.size(), false); for (int i = ; i < s.size(); ++i) {
if (wordDict.find(s.substr(, i+)) != wordDict.end()) {
seg_flag[i] = true;
continue;
} for (int j = i; j >= ; --j) {
if (seg_flag[j-] == true && (wordDict.find(s.substr(j, i - j + )) != wordDict.end())) {
seg_flag[i] = true;
break;
}
}
} return seg_flag[s.size() - ];
}
};

【Leetcode】【Medium】Word Break的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 【leetcode刷题笔记】Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  7. 【LeetCode算法-58/66】Length of Last Word/Plus One

    LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...

  8. 【LeetCode每天一题】Word Search(搜索单词)

    Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from le ...

  9. 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  10. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. SqlServer索引优化 查看碎片情况

    本文引自 DBCC DBREINDEX重建索引提高SQL Server性能 查看碎片情况使用  dbcc showcontig 函数来进行 代码: --改成当前库 use DB_Name --创建变量 ...

  2. Jmeter创建web测试计划

    创建web测试计划 在这一章,我们将学习如何创建基本的测试计划来测试一个web网站.您将创建五个用户并发送请求到JMeter网站的两个页面.同时,设置用户运行测试两次.因此,请求的总数是(5个用户)x ...

  3. Java 二叉树一些基本操作

    求二叉树中节点个数: /*1. 求二叉树中的节点个数 递归解法: (1)如果二叉树为空,节点个数为0 (2)如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1 */ pu ...

  4. 计算两个NSDate之间,相隔多少秒数

    计算两个NSDate之间,相隔多少秒数 //两个时间间隔秒数 - (NSInteger)intervalSecondsWithSmallDate:(NSDate*)smallDate bigDate: ...

  5. sqlserver 自增ID插入指定数据(转)

    set identity_insert 表名 ON --允许对自增列Id插入指定数据 insert into table_name(Id,Name) values(1,'test') set iden ...

  6. 从 JDK 源码角度看 Object

    Java的Object是所有其他类的父类,从继承的层次来看它就是最顶层根,所以它也是唯一一个没有父类的类.它包含了对象常用的一些方法,比如getClass.hashCode.equals.clone. ...

  7. 创建WPF用户控件

    wpf用户自定义控件和winform创建方法类似,这里先纠正一个误区,就是有很多人也是添加,然后新建,然后是新建用户控件库,但是为什么编译好生成后Debug目录下还是只有exe文件而没有dll文件呢? ...

  8. 告别Flash——那些年我们追过的FusionCharts

    随着FusionCharts最终放弃Flash这块蛋糕,不.或者已经不能叫做蛋糕了,现在Flash图表控件就只剩下AnyChart这一个独苗了,到底Flash还能走多远?这是Flash的末路吗? 众说 ...

  9. 001.開始使用ASP.NET Web API 2(一)

    原文鏈接:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...

  10. Java - 深拷贝技巧

    先让我描述一下问题:我在某Action(struts2.x)-A中写了一个功能P,以当前用户的某个标识F == 1时需要走这个功能,而且这个功能因某些原因已经侵入到了其他一些method中.顺便一提, ...