LeetCode——Word Break
Question
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".
Solution
用动态规划求解,遍历所有长度,看当前长度i是否可以完全分化。 长度为i的字符串分为两部分,前一部分是已经算过的是否可以划分的,只需要在字典中找后面一部分是否存在。
时间复杂度为O(n^2),空间复杂度为O(n).
Code
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
vector<bool> dp(s.length() + 1, false);
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j <= i; j++) {
if (dp[j]) {
string str = s.substr(j, i - j);
if (dict.find(str) != dict.end()) {
dp[i] = true;
break;
}
}
}
}
return dp[s.length()];
}
};
LeetCode——Word Break的更多相关文章
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- 升级个人网站框架组件IBatisNet+Castle
<sqlMap namespace="Sequence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance& ...
- html5 canvas 详细使用教程 转
分类: html5(9) 原文地址:http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html 原作很强悍 导航 前言 基本知识 绘 ...
- ubuntu16.04搭建jdk1.8运行环境
搭建环境:Ubuntu 16.04 ×64 JDK :jdk-8u171-linux-x64.tar.gz 首先下载linux对应的安装包下载地址:http://www.oracle.com/tech ...
- C#中遍历ArrayList的三种方法
using System; using System.Collections; using System.Linq; using System.Text; namespace ArrayListDem ...
- 本地代码推送到github仓库
git 初始化 cd 到需要提交的项目目录下,执行git init 配置用户名和邮箱 git config --global user.name "codingID" git co ...
- 基于GTID的主从架构异常处理流程
通常情况下我们主库的binlog只保留7天,如果从库故障超过7天以上的数据没有同步的话,那么主从架构就会异常,需要重新搭建主从架构. 本文就简单说明下如何通过mysqldump主库的数据恢复从库的主从 ...
- sqlalchemy笔记
http://jzqt.github.io/2015/12/29/SQLAlchemy%E7%AC%94%E8%AE%B0/ 用SQLAlchemy做ORM也有一段时间了,总结一下用过的一些地方. 连 ...
- 解决 apt-get the following packages has unmet dependencies 问题
安装vpn遇到以下问题: 显示flinux print util和openconnect存在依赖库的冲突 此时尝试安装新的tk.vpnc-scripts.libopenconnect5,尝试apt-g ...
- 解决跨域HttpResponseJsonCORS, HttpResponseCORS 返回字典数据
#!/usr/bin/python # -*- coding: UTF-8 -*- import json from django.http import HttpResponse def HttpR ...
- 初级dba学习之路参考
今天周一拖着疲惫的身躯 11点才离开公司,回到家估计写完这篇博客就要17号了. 一个人走在回家的路上,很黑,突然很多感触,一个人在北京拼搏,不敢停止学习的脚步,因为只要停下来就会感觉到孤独. 回顾一下 ...