139. Word Break(动态规划)
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because"leetcode"can be segmented as"leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because"applepenapple"can be segmented as"apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false

dp[i] 0-i这个字符串是否满足wordbreak
dp[0] = 0
dp[1] =dp[0] && s[0:1] in dict
dp[2] =dp[0] && s[0:1] in dict || dp[1] && s[1:2] in dict
dp[3] =dp[0] && s[0:3] in dict || dp[1] && s[1:3] in dict || dp[2] && s[2:3] in dict
dp[i] =dp[0] && s[0:i] in dict || ,,,,,,,||dp[i-1]&& s[i-1:i] in dict
Time complexity O(n^2)
Space complexity O(n)
语法注意:
s = "abcde"; 要得到”ce“
string word = s.substr(2,2);
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.size()+,false);
unordered_set<string>dict(wordDict.begin(),wordDict.end());
dp[] = true;
for(int i = ; i<=s.size();i++){
for(int j = ;j<i;j++){
string word = s.substr(j,i-j);
if(dp[j]&&dict.find(word)!=dict.end()){
cout<<word<<endl;
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
};
参考:
http://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/
139. Word Break(动态规划)的更多相关文章
- 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
原题地址 与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] 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 动态规划之139 Word Break
题目链接:https://leetcode-cn.com/problems/word-break/ 参考链接:https://blog.csdn.net/c_flybird/article/detai ...
- Word Break(动态规划)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
随机推荐
- 图->连通性->最小生成树(普里姆算法)
文字描述 用连通网来表示n个城市及n个城市间可能设置的通信线路,其中网的顶点表示城市,边表示两城市之间的线路,赋于边的权值表示相应的代价.对于n个定点的连通网可以建立许多不同的生成树,每一棵生成树都可 ...
- base64简单使用
加密: import base64 import random str ="aqwertyuiopasdfghjklzxcvbnm963.0852741,.;'" a = '人生苦 ...
- MySQL InnoDB加锁超时回滚机制(转)
add by zhj: 看来我对MySQL的理解还有待深入,水还是挺深的啊,MySQL给记录加锁时,可以通过innodb_lock_wait_timeout参数设置超时时间, 如果加锁等待超过这个时间 ...
- 在同一台电脑上部署多个tomcat服务器
因为在写一些小的项目的时候,需要另外用到一台图片服务器,所以不得不开启多个tomcat了. 在这里我用的是tomcat 9.0,一个是正常时的tomcat,一个是图片服务器,在这里我就用tomcat1 ...
- 前端 HTML body标签相关内容 常用标签 标题标签 h1-h6
标题标签 h1~h6 <h1> - <h6> 标签可定义标题.<h1> 定义最大的标题.<h6> 定义最小的标题. 由于 h 元素拥有确切的语义,因此请 ...
- 20170724 Airflow官网资料学习
-- 1 Apache Airflow 文档 AirFlow 对编程人员来讲就是一个平台,用于进行日程安排和监控.但是还在卵化期,严格来说,不是一个完整的成品.
- Java中的boxing和unboxing(转)
测试代码: System.out.println(0L == 0);//true System.out.println(((Long)0L).equals(0));//false Integer i1 ...
- python的mutable变量与immutable变量
python的变量分为mutable(可变的)和immutable类型. mutable:dict, list immutable:int , string , float ,tuple..
- 50A
#include <iostream> using namespace std; int main() { int m, n; cin>>m>>n; cout< ...
- Spring MVC定时服务
spring-mvc-config.xml <context:component-scan base-package="com.bf" ></context:co ...