139. Word Break

字符串能否通过划分成词典中的一个或多个单词。

使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词。

j表示的是以当前i的位置往前找j个单词,如果在j个之前能正确分割,那只需判断当前这j单词能不能在词典中找到单词。j的个数不能超过词典最长单词的长度,且同时不能超过i的索引。

初始化时要初始化dp[0]为true,因为如果你找第一个刚好匹配成功的,你的dp[i - j]肯定就是dp[0]。因为多申请了一个,所以dp中的i相当于s中的i-1。

s.substr(i-j,j)实际上是s.substr(i-j + 1 - 1,j),因为本身应该提取i - j +1,但因为dp位置比s多一个,所以还要-1。

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int length = ;
for(string word : wordDict){
if(word.size() > length)
length = word.size();
}
vector<bool> dp(s.size() + ,false);
dp[] = true;
for(int i = ;i <= s.size();i++){
for(int j = ;j <= length && j <= i;j++){
if(dp[i-j]){
string str = s.substr(i-j,j);
for(string word : wordDict){
if(word == str){
dp[i] = true;
break;
}
}
}
}
}
return dp[s.size()];
}
};

140. Word Break II

一个字符串加空字符串还是原字符串

class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
unordered_map<string, vector<string>> m;
return helper(s, wordDict, m);
}
vector<string> helper(string s, vector<string>& wordDict, unordered_map<string, vector<string>>& m) {
if (m.count(s)) return m[s];
if (s == "") return {""};
vector<string> res;
for (string word : wordDict) {
if (s.substr(, word.size()) != word) continue;
vector<string> rem = helper(s.substr(word.size()), wordDict, m);
for (string str : rem) {
res.push_back(word + (str.empty() ? "" : " ") + str);
}
}
m[s] = res;
return res;
}
};

leetcode 139. Word Break 、140. Word Break II的更多相关文章

  1. python如何转换word格式、读取word内容、转成html

    # python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...

  2. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

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

  4. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  5. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  6. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  7. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  8. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  9. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

随机推荐

  1. Redis一主二从Sentinel监控配置

    本文基于Redis单实例安装安装.https://gper.club/articles/7e7e7f7ff7g5egc4g6b 开启哨兵模式,至少需要3个Sentinel实例(奇数个,否则无法选举Le ...

  2. QT--初识

    一.组成QT的基本模块 二.创建一个简单工程 1.工程结构介绍 .pro 文件 QT += core gui # 包含的模块 greaterThan(QT_MAJOR_VERSION, ): QT + ...

  3. 初识python多线程

    目录 GIL锁 Thread类构造方法 Lock类.Rlock类 参考: python3多线程--官方教程中文版 python多线程-1 python多线程-2.1 python多线程-2.2 pyt ...

  4. Kotlin继承与重写重要特性剖析

    继续Kotlin的面向对象之旅. 继承: 在Java中我们知道除了final类不能被继承,其它的情况都是可以被继承的,而在Kotlin中的规则是这样的:“在Kotlin中,所有类在默认情况下都是无法被 ...

  5. 微信小程序API~地理位置location

    (1)使用微信内置地图查看位置 wx.openLocation(Object object) 使用微信内置地图查看位置 参数 Object object 属性 类型 默认值 必填 说明 latitud ...

  6. 清除PLSQL Developer访问数据库连接的历史记录

    1.C盘下 路径: C:\Users\JourneyOfFlower\AppData\Roaming\PLSQL Developer 12\Preferences\JourneyOfFlower\ J ...

  7. test20190829 神大校赛模拟

    100+100+0=200,聪明搬题人题面又出锅了. 最短路径(path) 给定有向图,包含 n 个节点和 m 条有向边. 一条A 到 B 的路径是最短路径当且仅当不存在另一条从A 到 B 的路径比它 ...

  8. strtol函数的用法——字符串转长整形

    /* strtol example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* strtol */ in ...

  9. java利用反射动态获取实体类的属性值

    直接贴代码吧,有需要的话,可以根据自己的需要修改部分代码: public BigDecimal getByAttributeName(ThmdGwqriR thmdGwqriR, String att ...

  10. [分享]Hidden Start - NTWind Software

    https://bbs.pediy.com/thread-229336.htm   [[other]] [分享]Hidden Start - NTWind Software 2018-6-29 09: ...