【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" >

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 )
时间: 2024-08-26 08:21:54

【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-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】sort list(python)

链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 == None: return head1 # head1 and head2 point to the same link list if head1 == head2: return head1 head = None tail = None # the small pointer point

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

【LeetCode】Word Break 动态规划

题目:Word Break 思路:将一个串可以划分的共有s.length+1个点,判断长为n的串是否能由字典中的词组成,则看之前有没有划分点能使其处于字典中 ,这样该问题 就分解为子问题的求解 所以可以使用动态规划 <span style="font-size:18px;">public class Solution { public boolean wordBreak(String s, Set<String> dict) { boolean[] tag =

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever