【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 ...
随机推荐
- InnoDB INFORMATION_SCHEMA Buffer Pool Tables
InnoDB INFORMATION_SCHEMA Buffer Pool Tables InnoDB INFORMATION_SCHEMA缓冲池表提供有关InnoDB缓冲池中页面的缓冲池状态信息和元 ...
- svn基本使用详情
1.增加(Add)先提到变更列表中,再commit到配置库中,选择新增文件,右键SVN菜单执行“Add“操作提交到”变更列表中”,然后右键SVN菜单执行”SVNCommit”提交到版本库中. 2.删除 ...
- LeetCode(74) Search a 2D Matrix
题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...
- 异常 Failed to bind NettyServer on /10.133.7.216:29105, cause: Failed to bind to: /0.0.0.0:29105
"C:\Program Files\Java\jdk1.7.0_80\bin\java" -agentlib:jdwp=transport=dt_socket,address=12 ...
- 65.什么是IOC?【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 这个小节吧,是无意当中看了一篇文章,觉得介绍的特别好,引用到我的博客中,让大家也乐下.那么他是怎么解说IOC的呢?看如下: 套用好莱坞的一句名言就 ...
- bzoj 1500 [NOI 2005] 维修数列
题目大意不多说了 貌似每个苦逼的acmer都要做一下这个splay树的模版题目吧 还是有很多操作的,估计够以后当模版了.... #include <cstdio> #include < ...
- POJ1256 Anagram
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu Submit Status Descript ...
- 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...
- msp430项目编程
msp430中项目---LED数码管显示 1.数码管介绍 2.代码(直接使用引脚驱动) 3.代码(使用译码器驱动) 4.项目总结 msp430项目编程 msp430入门学习
- centos7 网络设置
1.显示所有连接的网络接口 ip link show 2.激活或禁止网络接口 sudo ip link set up/down {dev} 3.将一个或多个IPv4地址分配给网络接口$ sudo ip ...