leetcode140
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的更多相关文章
- [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 ...
- LeetCode140:Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- leetcode140 Word Break II
思路: 直接爆搜会超时,需要使用记忆化搜索.使用map把已经计算过的情况记录下来,避免重复计算. 实现: class Solution { public: vector<string> w ...
随机推荐
- Sublime Text 3中文乱码问题解决(最新)
Sublime Text 3是我MacBook Pro最喜欢的代码编辑器,没有之一,因为她的性感高亮代码配色,更因为它的小巧,但是它默认不支持GBK的编码格式,因此打开GBK的代码文件,如果里面有中文 ...
- 修改 App.Config 配置文件 C#
[转]在WCF程序中动态修改app.config配置文件 今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google ...
- VS2008--VS2013 各种版本官方下载地址
很多刚入门的学生都不知道从哪里下载Visual studio 编译器 , 我特闲的纯手工整理了下 Visual Studio 2005 Professional 官方90天试用版 英文版:http:/ ...
- [UE4]使用name slot制作带背景的容器
name slot相当于asp.net模板中的content模板. 可以往“Name Slot”里面拖放控件,如果没有“Name Slot”就不能给“BgPanel”拖放任何控件
- Linux常用命令1-50(持续更新中)
1:echo $PATH (打印出PATH变量的值) 不同用户下面的PATH值有可能不一样 echo 有显示打印的意思 $ 表示后面的是一个变量的意思 PATH 变量 /usr ...
- SAS 通过逻辑库引用名实现相关联
SAS逻辑库是可以有多个物理位置的 下面的代码示例给出了下定义多个物理位置的SAS逻辑库Y2014. 这3段代码都能实现将逻辑库引用名:Y2014与4个物理位置: e:\sas\data\Quater ...
- 01-Socket服务器
package com.day1; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExc ...
- 小朋友学C语言(5):常量和变量,for循环
动手编写程序: #include <stdio.h> int main() { int a = 1; printf("a = %d\n", a); a = 2; pri ...
- es6基础(7)--函数扩展
{ //有默认值的后面如果有参数必须要有默认值 function test(x,y="world"){ console.log(x,y) } test('hello');//hel ...
- linux运行.sh命令
# chmod +x AAA.sh 授权 # ./AAA.sh 运行