[LeetCode]题解(python):139-Word Break
题目来源:
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的更多相关文章
- LeetCode题解:(139) Word Break
题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 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 ...
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 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 ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 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 ...
随机推荐
- JAVA-反射-getGenericSuperclass()
1 public class Person<T> { 2 3 } 4 5 import java.lang.reflect.ParameterizedType; 6 import java ...
- iOS堆栈-内存-代码在据算机中的运行
其实作程序不管是那行,学什么语言最终的目的是和就算机打交道的,我们写的程序计算机是怎么处理的呢??? 计算机运行我们的程序无非就是吧磁盘-内存-cpu三者结合起来 我们写一个程序代码肯定是在此盘中存着 ...
- 通过自定义注解反射生成SQL语句
----------------------------------------Program.cs---------------------------------------- using Sys ...
- std::string 字符替换函数
// 替换路径中所有“\”为“/” #include <algorithm> static std::string ConvertSlash(std::string& strUrl ...
- boa,thttp服务器安装,配置,测试
boa 1, SERVER_ROOT自定义,define.h头文件中,默认“/etc/boa" 2,./configure 3,修改CC,默认CC=gcc,make 4,error util ...
- 复习-C语言内嵌汇编-初级(2)
汇编取出内存中的值 # include <stdio.h> int main() { ; ; int *p = &i; //ret = *p; __asm__( "ldr ...
- Python 正则表达式应用【转载】
将从正则表达式开始讲Python的标准库.正则表达式是文字处理中常用的工具,而且不需要额外的系统知识或经验.我们会把系统相关的包放在后面讲解. 正则表达式(regular expression)主要功 ...
- SQL Server MySQL 中的 in 与 null
例子: create table t(x int,y int); insert into t(x,y) values(1,1),(2,2),(null,null); 查询一: select x,y f ...
- Oracle EBS-SQL (GL-1):从总帐追溯到接收
SELECT je_header_id, je_line_num, trx_class_name, trx_type_name, trx_number_displayed, trx_date,comm ...
- 提高Delphi的编译速度(bpl和bcp)
delphi的编译速度提高(一) 此博文为原创,转载请注明出处 作者 :二娃 此博文的内容我曾经回答群内和论坛内的网友提问时回答过,现在写第一部分,第二部分,我再给出一个终极的提高速度的方法 我用过d ...