题目来源:

  https://leetcode.com/problems/word-break/


题意分析:

  给定一个字符串s和一个字典dict,判断s是不是由字典dict里面的元素组成的。


题目思路:

  这里可以用动态规划的思想。首先用一个tq[] 存储所有s[:i] 可以由dict组成的下标。如果存在s[tq[i] : j + 1] in dict,那么将j + 1加入tq,如果size在tq里面,那么返回True,否者返回False。


代码(python):

class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: Set[str]
:rtype: bool
"""
size = len(s)
if size == 0:
return True
tq = [0]
i = 0
while i < size:
j,nsize = 0,len(tq)
while j < nsize:
if s[tq[j]:i+1] in wordDict:
if i + 1== size:
return True
tq.append(i+1)
break
j += 1
i += 1
return False

[LeetCode]题解(python):139-Word Break的更多相关文章

  1. LeetCode题解:(139) Word Break

    题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...

  2. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  3. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  4. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  6. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  7. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  8. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

  9. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  10. leetcode 139. Word Break ----- java

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

随机推荐

  1. js中跳转

    <li><a href="javascript:recordRescSifting('+subject.subId+');">'+subject.subNa ...

  2. echarts的使用总结;

    题外话:好久没来博客园了,这几个月自己的工作经历可以算是相当丰富,其实一直不知道自己做web前端能做到什么时候,但是想说既然现在还在做着这个职位,就好好的学习.之前很少写js代码,来了新公司大多数都是 ...

  3. spring常量值注入

    <context:property-placeholder location="classpath:resources/*.properties" /> @Value( ...

  4. MYSQL 数据表备份

    方法: mysqldump -uuser_name -p db_name table_nameList >save_file_path 例子: 备份分studio数据库的student 表 my ...

  5. 百度地图Label 样式:label.setStyle

    创建文本标注对象设置样式的时候,其中的backgroundColor属性居然还支持透明啊,不过改变数值好像对效果没有影响 var numLabel = new BMap.Label(num); num ...

  6. 限定只能处理"A仓"和"B仓"入库

    应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name INV_INVTTMTX_MISC 表单名 Form Name INVTTMTX 说明 ...

  7. asp.net数据库操作类(二)

    第二版的数据库访问类出炉了:  C# Code  123456789101112131415161718192021222324252627282930313233343536373839404142 ...

  8. MFC非模态对话框销毁

    非模态对话框需要重载OnCanel方法, 并调用DestroyWindow, 且不能调用基类的OnCanel重载PostNcDestroy, 需要delete掉this指针 // Overrides  ...

  9. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  10. Android 4.2启动代码分析(一)

    Android系统启动过程分析 Android系统的框架架构图如下(来自网上):   Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...