思路是这种。我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在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)的更多相关文章

  1. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  2. 【leetcode】sort list(python)

    链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 ...

  3. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【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 ...

  5. 【leetcode】Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  7. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  8. 【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 ...

  9. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. cobbler 无人值守-介绍

    cobbler 介绍 快速网络安装linux操作系统的服务,支持众多的Linux版本,也支持网络安装windows系统 PXE的二次封装,将多种安装参数封装到一个菜单 它是由Python编写的 还可以 ...

  2. IDM下载器使用方法详解:百度网盘下载,视频会员一网打尽!

    一. IDM的设置 [01]IDM插件与各大浏览器的集成 默认情况下,在成功安装IDM后,直接点击这里的选项,会弹出[常规设置],一般情况下直接保持默认的配置即可,如果你使用的是比较小众的浏览器,你可 ...

  3. Postfix mail for azengna.com loops back to myself -solve

    设置 /etc/postfix/main.cf 原配置 mydestination = $myhostname, localhost.$mydomain, localhost 改为 mydestina ...

  4. 【HIHOCODER 1576】 子树中的最小权值(线段树维护DFS序)

    描述 给定一棵N个节点的树,编号1~N.其中1号节点是根,并且第i个节点的权值是Vi. 针对这棵树,小Hi会询问小Ho一系列问题.每次小Hi会指定一个节点x,询问小Ho以x为根的子树中,最小的权值是多 ...

  5. 分享一段代码,关于List

    @SuppressWarnings("unchecked") public List<LogisticsOrderType> getAllLogisticsOrderT ...

  6. iOS第三方地图-百度地图常用功能使用(POI搜索,地理正反编码,定位,添加标注)

    百度地图官网: http://developer.baidu.com/map/index.php?title=iossdk 百度地图集成 1.引入相关包

  7. BZOJ 1221: [HNOI2001] 软件开发【最小费用最大流】

    Description 某软件公司正在规划一项n天的软件开发计划,根据开发计划第i天需要ni个软件开发人员,为了提高软件开发人员的效率,公司给软件人员提供了很多的服务,其中一项服务就是要为每个开发人员 ...

  8. 【BFS+优先级队列】Rescue

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/I [题意] 给定一个n*m的迷宫,A的多个小伙伴R要去营救A,问需要时间最少的小 ...

  9. springboot注释详解

    1.属性注入 @ConfigurationProperties(prefix="...") spring会从classpath下的/config目录或者classpath的根目录查 ...

  10. delete NULL

    面试某公司的时候,某技术主管说你这delete前没有判断指针是否为NULL,当时我就…… 看来这是个传说中的好习惯,每当delete一个指针的时候,要先判断是不是NULL.比如: if ( p != ...