思路是这种。我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在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. CSS--基础块级元素与内联元素

    在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素.在HTML和XHTML中,块级元素不能继承自行内元素(即不能嵌套在行内元素),<p&g ...

  2. 基于tiny4412的u-boot移植(二)(转)

    http://www.cnblogs.com/pengdonglin137/archive/2015/12/27/5080645.html

  3. 洛谷2016 战略游戏 (0/1状态的普通树形Dp)

    题意: 给出一个树,覆盖树上某一个点的花费为w[i],求树上每一条边至少有一个点覆盖的最小花费. 细节: 1.一条边的两端可以均被覆盖,但是不能存在一条边的两端都不被覆盖. 2.可能存在 分析: 对于 ...

  4. java环境配置——jdk8

    在官网下载最新版本的jdk 测试版本:jdk-8u60-windows-x64.exe 测试环境:Windows Server 2012 R2 Standard  X64 开始执行安装 安装过程中会选 ...

  5. 什么是Istio

    本文主要是对Istio Prelim 1.0(https://preliminary.istio.io/docs/)的翻译 Istio:一种开放式平台,用于连接,管理和保护微服务. Istio提供了一 ...

  6. [luoguP2801] 教主的魔法(二分 + 分块)

    传送门 以为对于这类问题线段树都能解决,分块比线段树菜,结果培训完才知道线段树是一种特殊的分块方法,有的分块的题线段树不能做,看来分块还是有必要学的. 对于这个题,先分块,然后另开一个数组对于每个块内 ...

  7. 调用BOS服务保存一个单据的简化示例

    IMetaDataService metadataService = ServiceHelper.GetService<IMetaDataService>(); // 加载元数据 Form ...

  8. unbuntu下安装多个JAVA JDK版本及如何切换

    当前环境已经安装过jdk1.6.0_45安装JDK 1.7.x时,若安装错误,可执行以下步骤:sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-ge ...

  9. 寒武纪camp Day4

    补题进度:7/11 A(博弈论) 略 B 待填坑 C(贪心) 题意: 一个序列是good的当且仅当相邻两个数字不相同.给出一个长度为n的数列,每个数字是ai.定义一种操作就是把a中某个元素拿到首位去, ...

  10. loj516 DP一般看规律(set启发式合并)

    题目: https://loj.ac/problem/516 分析: 每次将一个颜色更改为另一个颜色相当于将两个集合合并 然后对于答案的更新,一个点插入到一个集合中,那么可能更新答案的就是其前驱节点或 ...