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 ...
随机推荐
- C/C++中字符串和数字互转小结
一. 数字 转 char*型 1.sprintf函数(适合C和C++) 示例: char str[50]; int num = 345; sprintf(str,"%d",num) ...
- C++11--Tuple类<tuple>
#include "stdafx.h" #include <iomanip> #include <condition_variable> #include ...
- 【Spring学习笔记-3】国际化支持
[Spring]国际化支持 一.总体结构: 两个国际化资源中的内容: 二.程序 2.1 配置Spring上下文 beans.xml文件 <?xml version="1.0" ...
- Ubuntu 14.10 下HBase错误集
1 如果机群时间不同步,那么启动子节点RegionServer就会出问题 aused by: org.apache.hadoop.hbase.ipc.RemoteWithExtrasException ...
- R语言——实验4-人工神经网络
带包实现: rm(list=ls()) setwd("C:/Users/Administrator/Desktop/R语言与数据挖掘作业/实验4-人工神经网络") Data=rea ...
- [转][C#]枚举的遍历Enum
// 加载所有颜色 //foreach (Color item in Enum.GetValues(typeof(Color))) foreach (var item in typeof(Color) ...
- Underscore.js(1.9.1) 封装库
// Underscore.js 1.9.1// http://underscorejs.org// (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and ...
- go语言学习--语法糖
语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有 ...
- kickstart
关闭防火墙.关闭selinux 1.配置DHCP服务 # yum install dhcp -y dhcp配置文件如下 # vi /etc/dhcp/dhcpd.conf 查看路径 # rpm -ql ...
- document.createRange剪贴板API
js实现复制到剪贴板 document.createRange() API 选中元素→range→selection是一一对应的,即选区必须连续,不可以有分开的多个区域.另外,被选元素必须在dom树上 ...