【leetcode】Word Break(python)
思路是这种。我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到。那么就是False了。
在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同。比方说word=‘ab’, dict={ 'a', ab', ...},在找到a后,接下来处理的是b。我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中。所以这里的每一个子串就能够有三种选择。要么自己单独作为个体到dict中找,要么就跟前面的结合起来进行找。要么就是等,等后面的新的字符。构成更长的子串。
可是还有问题,上面我们说的是跟前面的结合起来找。那么这个前面是个什么定义?开头?开头后的第一个? 第二个?所以我们要记录一些信息。来表示前面的子串,是从哪里断开,从而满足条件的。那么我们就能够依次与离当前子串近的部分进行结合。比方:word = ’aab‘, dict = { a, aab }。处理a时。是满足的,下一个a。也是满足的,处理 b 时,b不在dict中,那么就与前面的a结合, 形成 ab,发现不在dict 中,那么继续与前面的结合,形成 aab,发如今dict 中,那么 word 总体就满足条件。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpcXV4aW5rb25n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
class Solution:
# @param s, a string
# @param dict, a set of string
# @return a boolean
def wordBreak(self, s, dict):
if len( s ) == 0 or len(dict) == 0:
return False
#初始长度为0,表示的是之前的元素已经搞定,能够从0開始继续向后
dp = [ 0 ]
# s串的长度,[1, len ( s )]。我们挨个去搞定兴许的断句
for i in range(1, len( s ) + 1):
#前面全部的合法的断句处,也就是全部的合法的起始点,由于若前面的都没搞定。不能够继续后面的
for j in xrange( len( dp ) - 1, -1, -1):
substr = s[dp[j] : i]
if substr in dict:
dp.append(i)
break
return dp[-1] == len( s )
【leetcode】Word Break(python)的更多相关文章
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】sort list(python)
链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 ...
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【leetcode】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- MySQL连接使用及分类
SQL连接 SQL 连接(JOIN)子句用于将数据库中两个或者两个以上表中的记录组合起来.连接通过共有值将不同表中的字段组合在一起. 考虑下面两个表,(a)CUSTOMERS 表:+----+---- ...
- 如何用纯 CSS 创作一个荧光脉冲 loader 特效
效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/erRzzR 可交互视频教程 此视 ...
- Java:清空文件内容
文章来源:https://www.cnblogs.com/hello-tl/p/9139432.html import java.io.*; public class FileBasicOperati ...
- 【BZOJ 1084】 [SCOI2005]最大子矩阵(DP)
题链 http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩 ...
- LeetCode01--两数之和
''' 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给 ...
- Android记录2013年10月20日
1. ailed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connect ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- AR+ 实时音视频通话,虚拟与现实无缝结合
今年中旬 Google 在万众期待下推出了 ARCore,能将现实与数码完美无缝地融合在一起,丰富我们的现实世界.通过它开发者可以更加快速方便地在 Android 平台开发 AR 应用,凭借 AR 技 ...
- 【转载】ubuntu16.04 无线/Wifi 上网速度慢的解决方法
原文链接:http://tieba.baidu.com/p/4737599703[侵删] 一直以为是域名解析的问题,可也觉得不像.今天在百度搜索“ubuntu16.04域名解析慢”的时候无意中看到了h ...
- CodeForces 595A Vitaly and Night
水题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...