class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: Set[str]
:rtype: List[str]
"""
return findWords(0, len(s), s, wordDict, {}) def findWords(start, end, s, wordDict, cache):
if start in cache:
return cache[start]
cache[start] = []
candidate = ''
current = start
while current < end:
candidate += s[current]
current += 1
if candidate in wordDict:
if current == end:
cache[start].append(candidate)
else:
for x in findWords(current, end, s, wordDict, cache):
cache[start].append(candidate + ' ' + x)
return cache[start]

leetcode140的更多相关文章

  1. [Swift]LeetCode140. 单词拆分 II | Word Break II

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

  2. LeetCode140:Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  3. leetcode140 Word Break II

    思路: 直接爆搜会超时,需要使用记忆化搜索.使用map把已经计算过的情况记录下来,避免重复计算. 实现: class Solution { public: vector<string> w ...

随机推荐

  1. pytest.8.使用pytest做简单的接口测试

    From: http://www.testclass.net/pytest/test_api/ 背景 我们现在需要测试v2ex网站的查看论坛节点信息的api.具体如下: 节点信息 获得指定节点的名字, ...

  2. spring cloud-前端跨域问题的解决方案

    当我们需要将spring boot以restful接口的方式对外提供服务的时候,如果此时架构是前后端分离的,那么就会涉及到跨域的问题,那怎么来解决跨域的问题了,下面就来探讨下这个问题. 解决方案一: ...

  3. code block自动生成makefile

    安装插件 http://developer.berlios.de/projects/cbmakegen/  然后会在project菜单下面有一个Generate Makefile的选项 点击就行了 如 ...

  4. 基于Kafka消息驱动最终一致事务(一)

    基本可用软状态最终一致事务 本用例分两个数据库分别是用户库和交易库,不使用分布式事务,使用基于消息驱动实现基本可用软状态最终一致事务(BASE).现在说明下事务逻辑演化步骤,尊从CAP原则,即分布式系 ...

  5. 新建本地仓库,同步远程仓场景,出现git branch --set-upstream-to=origin/master master 解决方法

    1.本地创建一个本地仓库 2.关联远程端:git remote add origin git@github.com:用户名/远程库名.git3.同步远程仓库到本地git pull这个时候会报错If y ...

  6. Java学习——多线程例子:李四王五

    package cys; public class Example9_2 { public static void main(String[] args) { // TODO Auto-generat ...

  7. 信息安全-加密:RSA 算法

    ylbtech-信息安全-加密:RSA 算法 RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制. 1.返 ...

  8. [转][MVC]更新 dll 后版本不匹配的问题

    <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken=" ...

  9. Oracle EXP-00091解决方法

    非交互式 windows: D:\>exp scott/tiger file=employee.dmp tables=(emp,dept) linux需要加双引号 EXP-00091: [ora ...

  10. DockerFile服务

    Dockerfile分为四部分:基础镜像信息.镜像创建者信息.镜像操作指令.容器启动执行指令. 一.Dockerfile的书写规则及指令使用方法 Dockerfile的指令是忽略大小写的,建议使用大写 ...