给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

说明:

  • 拆分时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。

示例 1:

输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。

示例 2:

输入: s = "applepenapple", wordDict = ["apple", "pen"] 输出: true 解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。   注意你可以重复使用字典中的单词。

示例 3:

输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] 输出: false

dp[i]表示长为i的从s的第一个字符开始的字串是否能匹配。

可以想为长度为i是否匹配需要看长度i - j是否匹配和是否有长度为j且合适正确的单词。

这样就形成了动态规划的公式。

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict)
{
int len = s.size();
vector<bool> dp(len + 1, false);
map<string, bool> check;
//i表示结束的位置,相当于end()方法;
for(int i = 0; i < wordDict.size(); i++)
{
check[wordDict[i]] = true;
}
dp[0] = true;
for(int i = 1; i <= len; i++)
{
for(int j = 0; j < i; j++)
{
if(dp[j] && check[string(s.begin() + j, s.begin() + i)])
{
dp[i] = true;
break;
}
}
}
return dp[len];
}
};

Leetcode139. 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. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

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

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

  5. [Leetcode] word break ii拆分词语

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

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

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

  7. LeetCode139:Word Break

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

  8. Java Word Break(单词拆解)

    给定一个字符串 String s = "leetcode" dict = ["leet", "code"]. 查看一下是够是字典中的词语组成 ...

  9. [LeetCode] 140. Word Break II 单词拆分II

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

随机推荐

  1. H5点击拨打电话,发短信

    phone(){ window.location.href = 'tel:123123'// 拨打电话 window.location.href = 'sms:123123'// 发短信 }

  2. AOP-面向切面编程-1

    将方法类比成一个积木,哪里需要执行插到哪里 视野角度就是将一个程序比作几条绳子的集合,每个集合是一堆方法的集合,那么把绳子截断,绳子的切面就是一堆方法中一个方法与另一个方法的交界处,将你需要的方法切入 ...

  3. cocos2D-X LUA 常用功能封装和工作经验的一些解决方案

    --[[ Packaging_KernelEngine.h 文件说明:所有对象在建立时位置是优先的,传入位置参数必须cc.p(X,Y) CurObj:表示要传入当前的对象 将3.10 lua api ...

  4. python queue, pipe, manage

    线程中的Queue import time import threading import queue import random def putMessage(): for i in "H ...

  5. python和go对比字符串的链式处理

    一.什么是链式处理 对数据的操作进行多步骤的处理称为链式处理,链式处理器是一种常见的编程设计,链式处理的开发思想将数据和操作拆分,解耦,让开发者可以根据自己的技术优势和需求,进行系统开发,同时将自己的 ...

  6. H5页面在手机上查看 使用手机浏览自己的web项目

    参考:http://www.browsersync.cn/#install 首先全局安装BrowserSync : npm install -g browser-sync 其次在项目文件夹下运行: b ...

  7. System.Web.Mvc.IController.cs

    ylbtech-System.Web.Mvc.IController.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Public ...

  8. PAT甲级——A1107 Social Clusters

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  9. 求一个n!中尾数有多少个零

    题目描述: 输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2 输入描述: 输入为一行,n(1 ≤ n ≤ 1000) 输出描述: 输出 ...

  10. java模拟页面表单登录

    简单的通过表单的post请求提交到后台 package testpostlogin; import java.io.BufferedReader; import java.io.File; impor ...