LeetCode OJ:Word Break
来源:互联网 发布:dbc数据库详解 编辑:程序博客网 时间:2024/06/06 23:28
Word Break
Total Accepted: 5921 Total Submissions: 31188My SubmissionsGiven 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"
.
算法思想:动态规划
dp[i]表示从i到len-1位置是否可以被分割成字典中的单词,所以,只要确定一组,当i<=j<len时,从i到j的子字串是字典中的单词,同时dp[j+1]=true,即j+1到len-1位置可以被分割,那么dp[i]就为true
class Solution{public:bool wordBreak(string s, unordered_set<string> &dict) {int len=s.length();vector<bool> dp(len+1,false);dp[len]=true;for(int i=len-1;i>=0;i--)for(int j=i;j<len;j++){string str=s.substr(i,j-i+1);if(dict.find(str)!=dict.end()&&dp[j+1]){dp[i]=true;break;}}return dp[0];}};
0 0
- LeetCode OJ:Word Break
- LeetCode OJ - Word Break
- LeetCode OJ Word Break
- LeetCode OJ:Word Break II
- LeetCode OJ - Word Break II
- LeetCode OJ——Word Break
- [LeetCode OJ] Word Break II 解题报告
- [LeetCode OJ] Word Break 解题报告
- LeetCode OJ 之 Word Break (断词)
- leetcode Word Break & Word Break ||
- LeetCode OJ 之 Word Break II (断词 - 二)
- Leetcode: Word Break
- [leetcode]Word Break
- [leetcode]Word Break II
- LeetCode:Word Break
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break
- 4.在二元树中找出和为某一值的所有路径(树)
- poj2823
- # winform listView鼠标拖拽到pictureBox问题的解决办法,鼠标拖动
- myeclipse配置hadoop开发环境,及遇到问题解决
- hdu 2844题解
- LeetCode OJ:Word Break
- POJ 1125 Stockbroker Grapevine
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- ACM POJ 1056 (IMMEDIATE DECODABILITY)
- 结束到开始之间
- 4.1 启动Acitivity
- 团队管理
- FJSDFZOJ 1109 统计数字
- 内联函数