给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词。你可以假设字典中无重复的单词。
例如,给出
s = "leetcode",
dict = ["leet", "code"]。
返回 true 因为 "leetcode" 可以被切分成 "leet code"。

详见:https://leetcode.com/problems/word-break/description/

Java实现:

  1. class Solution {
  2. public boolean wordBreak(String s, List<String> wordDict) {
  3. int n=s.length();
  4. //dp[i]表示前i个字符能不能被dict完美划分
  5. boolean[] dp=new boolean[n+1];
  6. dp[0]=true;
  7. for(int i=1;i<=n;++i){
  8. for(int j=0;j<i;++j){
  9. //substring是前闭后开
  10. String tmp=s.substring(j,i);
  11. if(dp[j]&&wordDict.contains(tmp)){
  12. dp[i]=true;
  13. break;
  14. }
  15. }
  16. }
  17. return dp[n];
  18. }
  19. }

参考:http://www.cnblogs.com/grandyang/p/4257740.html

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, determine ...

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

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

  3. [leetcode]139. Word Break单词能否拆分

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

  4. Leetcode139. Word Break单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

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

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

  6. 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  ...

  7. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. 139. Word Break

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

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

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

随机推荐

  1. kafka 和 zookeeper 常用命令记录

    启动zookeeper zkServer.sh start 启动kafka服务器 kafka-server-start.sh /software/kafka_2.10-0.10.2.1/config/ ...

  2. 浏览器和服务器 对post get请求 url长度限制

    1. URL长度限制 2. Post数据的长度限制 3. Cookie的长度限制 1. GET  URL长度限制 在Http1.1协议中并没有提出针对URL的长度进行限制,RFC协议里面是这样描述的, ...

  3. python selenium实现百度搜索

    1.环境 python2.7+selenium+phantomjs+linux 2.代码 #-*-coding:utf-8 -*- from selenium import webdriver fro ...

  4. 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)

    1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则)     * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...

  5. Couldn't connect to host, port: smtp.163.com, 25; timeout -1;

    运行出现以下报错: Couldn't connect to host, port: smtp.163.com, 25; timeout -1; 也要设置端口 spring.mail.port=25

  6. mysql 5.5升级到5.7版本操作流程

    一.备份原来 phpStudy 中 MySQL 安装目录 二.把下载的 MySQL 压缩文件解压至 phpStudy 下的 MySQL目录,复制 my-default.ini ,重命名为 my.ini ...

  7. hadoop2.x安装配置

    1.首先准备hadoop2.2.0的安装包,从官网获取,略. 2.加压安装包,进行配置.假设hadoop安装到/usr/hadoop-2.2.0目录,则进行如下配置: (1)/etc/profile配 ...

  8. codeforces 414A A. Mashmokh and Numbers(素数筛)

    题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...

  9. Jmeter学习之While Controller

    参考 https://www.cnblogs.com/richered/p/8404641.html https://blog.csdn.net/rwang99/article/details/511 ...

  10. ecb-2.40与cedet-1.1的兼容(转载)

    转自:http://blog.csdn.net/cnsword/article/details/7474119 今天凑热闹把fedora升级到了17,emacs升级到了24,但是悲剧了,显示cedet ...