[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 ...
随机推荐
- EassyMock实践 捕获参数
在测试接口过程中,有时我们希望知道自己期望传入的参数是什么,以此来判断传入参数的正确行,这时就需要用到EassyMock的capture方法.该方法能捕获传入的参数存放到自定义的变量中,然后用捕获的参 ...
- Java学习之Comparable与Comparator的区别
Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序 ...
- OC语法1——OC概述
Object-C简介: OC,即Object-C,iOS开发的核心语言.它是基于C语言的,在C的基础上做了面向对象的封装,所以OC是面向对象的语言.同时也因此OC是兼容C的,也就是说在iOS开发中,可 ...
- git彻底删除commit记录的方法
在github上,如果非默认分支的话,直接用以下方法: git reset --hard <commit_id> git push origin HEAD --force 如是默认分支,需 ...
- sqlserver 分页sql语句
select * from (select *,row_number() over(order by CONTENT_ID ) as rnum from ArchiveContents) t whe ...
- ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句
ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句 存储过程 1.找到正在执行的存储过程的 sid ,serial# select b.sid,b.SERIAL#,a.OBJEC ...
- ThinkPHP中 按条件查询后列表显示
最近在项目中遇到了需要根据下拉框的条件筛选出符合条件的数据,然后进行列表显示的问题. 在ThinkPHP中进行列表显示的传统过程:通过在后台控制器中查询出数据,然后通过$this->assign ...
- J2SE知识点摘记-数据库(一)
一. 数据库连接 在JDBC的操作过程中,进行数据库连接的主要步骤如下: 通过Class.forName()加载数据库的驱动程序.首先需要利用来自Class类中的静态方法forNam ...
- Android UI学习组件概述
Android的UI组件繁多,如果学习的时候不能自己总结和分类而是学一个记一个不去思考和学习他们内在的联系那真的是只有做Farmer的命了.为了向注定成为Farmer的命运抗争,在学习Android的 ...
- 布线问题(prime)
布线问题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...