[LeetCode]题解(python):139-Word Break
题目来源:
https://leetcode.com/problems/word-break/
题意分析:
给定一个字符串s和一个字典dict,判断s是不是由字典dict里面的元素组成的。
题目思路:
这里可以用动态规划的思想。首先用一个tq[] 存储所有s[:i] 可以由dict组成的下标。如果存在s[tq[i] : j + 1] in dict,那么将j + 1加入tq,如果size在tq里面,那么返回True,否者返回False。
代码(python):
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: Set[str]
:rtype: bool
"""
size = len(s)
if size == 0:
return True
tq = [0]
i = 0
while i < size:
j,nsize = 0,len(tq)
while j < nsize:
if s[tq[j]:i+1] in wordDict:
if i + 1== size:
return True
tq.append(i+1)
break
j += 1
i += 1
return False
[LeetCode]题解(python):139-Word Break的更多相关文章
- LeetCode题解:(139) Word Break
题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 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 ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- EF实体框架-从数据库更新模型 一部分表的外键(导航属性)无法显示
从数据库更新模型 要想让数据库表之间的外键关系 显示到实体模型的导航属性中去. 表的外键 对应另一张表的字段要是主键,唯一键显示不出来
- Editplus配置java运行环境
Editplus配置java运行环境 下载及安装: editplus官网下载地址:https://www.editplus.com/ 安装方法和安装普通exe应用程序一样,选在安装路径,下一步下一步, ...
- java中的堆、栈、常量池
java中的堆.栈.常量池 分类: java2010-01-15 03:03 4248人阅读 评论(5) 收藏 举报 javastring编译器jvm存储equals Java内存分配: 1. 寄存器 ...
- 走进C标准库(8)——"string.h"中函数的实现相关字符串操作函数
我的strcat: char *strcat(char *dest,char *src) { char * reval = dest; while(*dest) dest++; while(*src) ...
- mybatis.generator.configurationFile
mybatis.generator.configurationFile 有一个更好的配置方法,可以不用在generateConfig.xml里面写死驱动的地址:如果你的mybatis连接也是在pom. ...
- Debug程序无法运行解决
说明:文章内容部分参考网络上的解决思路. 在没有安装Microsoft Visual Studio的系统上,Debug版本无法正常运行.这是由于缺少vs运行时库引起的. 以vs2005为例.开发机用v ...
- 2016 Multi-University Training Contest 5&6 总结
第五场和第六场多校都打得很糟糕. 能做到不以物喜不以己悲是假的,这对队伍的情绪也可以算上是比较大的打击. 很多时候我们发现了问题,但是依旧没有采取有效的方法去解决它,甚至也没有尝试去改变.这是一件相当 ...
- Linux-storage-stack-diagram
just a diagram 一目了然. 对于isci 只是用过LIO和STGT 两种后端. 这里有各种后端的比较. http://scst.sourceforge.net/comparison.ht ...
- openstack core components use 总结
1,附加volume(块存储,云硬盘)到vmInstances(虚拟机实列)
- applet部署,无需修改客户端设置。
1 开发applet程序,编译成jar包 2 给jar包做数字签名: (1).用keytool生成密钥: keytool -genkey -keystore myapplet.keystore - ...