题目链接:https://leetcode-cn.com/problems/word-break/

参考链接:https://blog.csdn.net/c_flybird/article/details/80703494

     http://www.cnblogs.com/springfor/p/3874731.html

这种题目一般出现字符串、子数组都需要使用动态规划

dp[i],前i个字符串是否在字典中。

dp[0]=true;空字符肯定是在字典中。

public boolean wordBreak(String s, List<String> wordDict) {
boolean dp[]=new boolean[s.length()+1];
Arrays.fill(dp, false);
dp[0]=true;
for (int i = 1; i < dp.length; i++) {
for (int j = i - 1; j >= 0; j--)
{
if (dp[j] && wordDict.contains(s.substring(j, i)))
{
dp[i] = true;
break;
}
} }
return dp[s.length()];
}

动态规划之139 Word Break的更多相关文章

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

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

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

  3. Leetcode#139 Word Break

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

  4. 139. Word Break

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

  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 解题报告(Python & C++)

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

  7. 139. Word Break(动态规划)

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

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

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

  9. 【LeetCode】139 - Word Break

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

随机推荐

  1. java多线程小题一瞥

    有如下线程类定义: public class MyThread extends Thread { private static int num = 0; public MyThread() { num ...

  2. Xception网络结构理解

    Xception网络是由inception结构加上depthwise separable convlution,再加上残差网络结构改进而来/ 常规卷积是直接通过一个卷积核把空间信息和通道信息直接提取出 ...

  3. LA 2218 Triathlon(半平面交)

    Triathlon [题目链接]Triathlon [题目类型]半平面交 &题解: 做了2道了,感觉好像套路,都是二分答案,判断半平面交是否为空. 还有刘汝佳的代码总是写const +& ...

  4. python3(socket 实现udp,tcp套接字编程)

    #coding=utf-8 #客户端程序TCP 连接 import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connec ...

  5. python-demo实例

    1.turtle库与蟒蛇案例 import turtle def drawSnake(rad,angle,len,neckrad): for i in range(len): turtle.circl ...

  6. META标签之关键词、网页描述设置帮助SEO网站优化(转)

      ASP.NET 4.0 Web Forms针对SEO改进措施中有一个是在Page类中加了2个新属性:MetaKeywords 和MetaDescription,它们使得在后台代码类中用编程的手法设 ...

  7. hdu3374 最大最小表示法 +kmp

    #include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...

  8. sql server 将两列的值合并到另一列

    select top 100 t2.FullName, * from Subject,(select id, isnull(first_name,'') +isnull(middle_name,'') ...

  9. KVM_webvirtmgr

    一.webvirtmgr安装前说明: 1:操作做系统:centos7.2_x86_64 2:安装参考出处1:https://github.com/retspen/webvirtmgr/wiki/Ins ...

  10. Linux Firewall 开启与关闭 以及sudo 设置

    Linux 系统下,普通用户经常需要使用root 用户的权限,所以要经常切换到root用户,比较麻烦,因此可以给普通用户添加root 权限,需要在常规命令前面加上sudo 切换到root vi  /e ...