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, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

法I: DP, 二位数组。用所求值“——是否可以被分段,作为状态。

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
//dp[i][j]: s[i...j] can be egemented in dict
//dp[i][j] = dp[i][k] && d[k][j]
int len = s.length();
vector<vector<bool>> dp(len,vector<bool>(len,false));
for(int i = ; i < len; i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()) dp[][i]=true;
for(int j = ; j <= i; j++){
if((wordDict.find(s.substr(j,i-j+))!=wordDict.end()) && dp[][j-]) dp[][i]=true;
}
}
return dp[][len-];
}
};

法II:发现只用到了dp[0][i], i = 0...len-1=>使用一维数组。

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
//dp[i]: s[0...i] can be egemented in dict
//dp[i] = dp[0][k] && d[k][i]
int len = s.length();
vector<bool> dp(len,false);
for(int i = ; i < len; i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()) dp[i]=true;
for(int j = ; j <= i; j++){
if((wordDict.find(s.substr(j,i-j+))!=wordDict.end()) && dp[j-]) dp[i]=true;
}
}
return dp[len-];
}
};

139. Word Break (String; DP)的更多相关文章

  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. 139. Word Break

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

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

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

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

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

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

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

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

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

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

  9. Leetcode#139 Word Break

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

随机推荐

  1. Bezier画线算法

    编译器:VS2013 描述:Bezier画线是利用导数相同拼接曲线,使曲线十分光滑,而不是随意拼接观赏性很差 主函数段 #include "stdafx.h" #include&l ...

  2. java学习——构造类之3!+5!=126

    package my_project; import java.util.Scanner; public class my_first_class { public static void main( ...

  3. 杂项:Juice UI

    ylbtech-杂项:Juice UI Juice UI是开源的 WebForms 控件集,是一个功能强大的框架,它可以给ASP .NET开发人员带来丰富的.可以作为易于使用的控件的jQuery UI ...

  4. 阿里云EC2+QEMU虚拟机+ROS完全教程!

    ---恢复内容开始--- 1.安装centos6.5 x64 同时记录,当前centos分配得到的IP,子网掩码,网关,以及MAC!!! 查看IP.mac命令ip add 查看网关命令cat /etc ...

  5. tcp_tw_recycle和tcp_timestamps导致connect失败问题

    把服务里面的net.ipv4.tcp_timestamps这个参数设置为0后已经可以正常telnet通了. 具体设置方法: 在/etc/sysctl.conf  里面加入 net.ipv4.tcp_t ...

  6. Unreal Engine 4(虚幻UE4)GameplayAbilities 插件入门教程(六)GameplayEffect的级别设置

    本节的内容不难,权当是复习.如果没有完成前面的教程,请前往学习. 第一步:用一个csv文件表示级别数据,下图中的Hurt随级别1~7表示其损伤值在1~7级别时分别是-7,-14,-20等.写好之后关闭 ...

  7. Lua语言中的__index,__newindex,rawget和rawset

    转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...

  8. express有中间件的增删改查

    var express = require('express');引入express框架 var router = express.Router();引入router路由级中间件 var data = ...

  9. jpa 一对一

    实体   Manager package entity; import javax.persistence.Column; import javax.persistence.Entity; impor ...

  10. 调用css文件,进行调色

    Title 小米       <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...