[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 ...
随机推荐
- NULL、空指针,’\0’,0的去区别
http://blog.csdn.net/bailyzheng/article/details/7677628参考该博客 NULL是空指针常量.
- android笔试题
1.请谈一下Android系统的架构. 答:Android系统采用了分层架构,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层. 2.谈谈android大众常用的五种布 ...
- Linux下多任务间通信和同步-消息队列
Linux下多任务间通信和同步-消息队列 嵌入式开发交流群280352802,欢迎加入! 简介 消息队列简称为队列.消息队列就是一些消息的列表.用户可以在消息队列中添加消息和读取消息等.从这点上看,消 ...
- Android listview 禁止滑动
listview.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionE ...
- iframe自适应高度的问题
最近工作中遇到了iframe自适应高度的问题. 如果在iframe中写定高度height的属性,并且iframe中内容高度小于给定的height时,会在手机浏览器中莫名的产生下拉框,造成体验度下降. ...
- HTML5兼容IE各版本的写法
IE下判断IE版本的语句 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> < ...
- 64位windows8的 IIS运行32位COM组件报错的解决
浏览时报错如下: Microsoft VBScript 运行时错误 错误 '800a01ad'ActiveX 部件不能创建对象: 'sqlcomp.FileSystemObject'/config.a ...
- 《转》读discuzx3.1 数据库层笔记
最近开始在看discuzx3.1的代码,看到数据库层的实现,discuzx的数据库层能够支撑数据库分库,分布式部署,主要水平分表,也可以很方便的支持其他数据库.性能上,可以做读写分离,支持数据缓存.可 ...
- 提醒录入BOM更改原因
应用 Oracle Bill Of Materiel 层 Level Function 函数名 Funcgtion Name BOM_BOMFDBOM 表单名 Form Name BOMFDBOM ...
- codeforces #286 Div.2 C DP总是以意外的方式打败我
题目大意:30001个岛排成一排,编号从0到30000,一共有n个宝物分散在这些岛上,一只猪最开始从0跳到d,之后每一步跳的步长和上一步相差不超过1,第二步步长就是d-1,d,d+1,第二步的位置就是 ...