【Leetcode】【Medium】Word Break
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【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 ' ' ...
- 【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 ...
- 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【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 ...
随机推荐
- Android可见APP的不可见任务栈(TaskRecord)销毁分析
Android依托Java型虚拟机,OOM是经常遇到的问题,那么在快达到OOM的时候,系统难道不能回收部分界面来达到缩减开支的目的码?在系统内存不足的情况下,可以通过AMS及LowMemoryKill ...
- CSAPP阅读笔记-数组分配与访问-来自第三章3.8的笔记-P176-P183
这一节比较简单,仅记录几个比较重要的点: 1.C语言允许对指针进行运算,计算出的值会根据该指针引用的数据类型大小进行伸缩. 例子: 其中,xE是数组的起始地址.注意,指针运算时,若最终结果为指针,则指 ...
- asp.net WebService技术简介
1.什么是web service? 这里借助百度百科专业的解释:web service是一个平台独立的.低耦合的.自包含的.基于可编程的web应用程序(说简单点,就是使用web service继续不需 ...
- Unity Github 项目收集
http://gad.qq.com/article/detail/24048 重磅推荐: Github 热门 Unity Assets 查询:http://unitylist.com/browse 最 ...
- Sed - An Introduction and Tutorial by Bruce Barnett
http://www.grymoire.com/unix/sed.html Quick Links - NEW Sed Commands : label # comment {....} Block ...
- codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】
Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 如何优雅的封装一个DOM事件库
1.DOM0级事件和DOM2级事件 DOM 0级事件是元素内的一个私有属性:div.onclick = function () {},对一个私有属性赋值(在该事件上绑定一个方法).由此可知DOM 0级 ...
- dns dig 查看支持ipv6网站
1.处理zone文件 A.先格式化区文件数据,去掉不需要的数据,生成新的文件 com.zone.sample cat com.zone |grep -P IN'\t'NS|awk -F '\t' '{ ...
- 「Sdchr 的邀请赛」题解
骗个访问量.. A:取石子 将点 x 与点 x / prime 连边,那么这个图可以由指数之和的奇偶性来划分成一个二分图. 接下来考虑推广阶梯 NIM (或者这原本就是阶梯 NIM ?),必胜当且仅当 ...
- 理解 Azure 虚拟机的性能监视
随着越来越多的用户将生产应用迁移到云平台,一些传统 IT 的运维功能也相应的需要改变,例如监控,备份等等.我们希望通过这一系列的文章来协助用户更好的理解在 Azure 云平台上实现资源监控的方法. 在 ...