https://oj.leetcode.com/problems/word-break-ii/

class Solution {
public:
unordered_set<string> dict;
string s;
unordered_map<string, vector<string> > memory; //用来记录一个string可以的组成 vector<string> wordBreak(string s, unordered_set<string> &dict) {
memory.clear();
this->dict = dict;
this->s = s; return subWordBreak(s);
}
vector<string> subWordBreak(string &s)
{
//先看是否之前处理过这个 子串
if(memory.find(s) != memory.end())
return memory[s]; // 新建一个 memory中的项,即返回值
vector<string> result; if(s.size() <= )
return result; // 广度,然后递归
// 个数
for(int i = ; i <= s.size(); i++)
{
// 找出所有合理的小片段,再递归剩下的片段 // if is a valid, belongs to dict
string subS = s.substr(,i); if(dict.find(subS) != dict.end())
{
if(i == s.size())
result.push_back(subS);
else
{
// 递归调用 剩下的部分
string leftstr = s.substr(i,s.size() - i);
vector<string> tmp = subWordBreak(leftstr);
for(int j = ; j < tmp.size(); j++)
{
string item = subS;
item.append(" ");
item.append(tmp[j]);
result.push_back(item);
}
}
}
}
memory.insert(make_pair(s,result));
return result;
}
};

LeetCode OJ--Word Break II ***@的更多相关文章

  1. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  2. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  3. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  4. leetcode 140. Word Break II ----- java

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

  5. LeetCode OJ——Word Break

    http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...

  6. Java for LeetCode 140 Word Break II

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

  7. 【leetcode】Word Break II (hard)★

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

  8. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

  9. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  10. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

随机推荐

  1. 排序算法C语言实现——插入排序(优于冒泡)

    为什么插入排序要优于冒泡? 插入排序在于向已排序序列中插入新元素,主要的动作是移动元素,涉及1次赋值,即data[j] = data[j-1]; 而冒泡排序在于相邻元素交换位置,涉及3条赋值,即iTm ...

  2. GIt-恢复进度

    继续暂存区未完成的实践 经过了前面的实践,现在DEMO版本库应该处于master分支上,看看是不是这样. $ cd /path/to/my/workspace/demo $ git status -s ...

  3. Python虚拟机类机制之对象模型(一)

    Python对象模型 在Python2.2之前,Python中存在着一个巨大的裂缝,就是Python的内置类type,比如:int和dict,这些内置类与程序员在Python中自定义的类并不是同一级别 ...

  4. Python协程详解(二)

    上一章,我们介绍了Python的协程,并讲到用yield达到协程的效果,这一章,我们来介绍yield from的结构和作用 我们先来对比下yield和yield from的用法 def first_g ...

  5. [网站公告]又拍云API故障造成图片无法上传(已恢复)

    大家好,18:00左右开始,又拍云API出现故障,调用图片上传API时出现错误:“The remote server returned an error: (403) Forbidden.”,造成图片 ...

  6. 【Maximal Rectangle】cpp

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  7. 文件处理之复杂,在于内置方法-----python

    抛砖引玉: 文件是我们储存信息的地方,我们经常要对文件进行读.写.删除等的操作,在Python中,我们可用Python提供的函数和方法方便地操作文件. ************************ ...

  8. 数组线性表ArrayList 和链表类LinkedList

    数组线性表类ArrayList 和链表类LinkedList 是实现List接口的两个具体类.ArrayList 数组储存元素,这个数组是动态创建的.如果元素个数超过了数组的容量,就创建一个更大的新数 ...

  9. 快速排序(Quick Sort)及优化

    原理介绍 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...

  10. 【转】Unity3D 射线Ray实现点击拾取

    游戏中经常会有鼠标移动到某个对象上来拾取它的功能,我们可以用Unity3D中的射线Ray实现这一效果.原理是在我们鼠标的位置,从屏幕射出一条射向世界空间的射线,当这条射线碰撞到我们需要拾取的对象时,我 ...