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. ubuntu MySQL拒绝远程连接(10061)

    MySQL是使用apt-get安装的 1.停止mysql服务 sudo service mysql stop 2.修改配置文件/etc/mysql/mysql.conf.d/mysqld.cnf 将b ...

  2. PAT 乙级 1049 数列的片段和(20) C++版

    1049. 数列的片段和(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定一个正数数列,我们可以从中截 ...

  3. 数据库SQL语言学习--上级练习1(数据查询)

    上机练习1 1.              启动SQL Server 2008中的 SQL Server Management Studio. 2.              创建数据库Student ...

  4. DP 01背包 七夕模拟赛

    问题 D: 七夕模拟赛 时间限制: 1 Sec  内存限制: 128 MB提交: 60  解决: 23[提交][状态][讨论版] 题目描述 " 找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手 ...

  5. IE浏览器强制不是要兼容视图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  6. [UE4]手持多把枪的位置调节

    如果一个角色有多把枪,但是骨骼插槽只有一个,可以通过直接在枪蓝图中调整枪的位置和旋转角度.

  7. sshd服务安装

    SSHD服务 介绍:SSH 协议:安全外壳协议.为 Secure Shell 的缩写.SSH 为建立在应用层和传输层基础上的安全协议. 作用:sshd服务使用SSH协议可以用来进行远程控制, 或在计算 ...

  8. [VS工具]远程在IIS附加调试代码

    1.首先在服务器以管理员的方式打开msvsmon.exe(一般这个文件路径:C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\ID ...

  9. 02-创建String对象

    创建一个String对象实在是太简单了,就是因为简单,所以有很多java程序员做了好几年的开发,也没有注意这些小细节问题 String字符串的本质就是char数据对象, 那么char[0]数组当中的一 ...

  10. SQLite3数据库

    #SQLite可视化管理工具(SQLite Expert Pro) SQLite特点: 1. 遵守ACID(原子性.一致性.隔离性和持久性)的关系型数据库管理系统:2. 不是一个C/S结构的数据库引擎 ...