LeetCode _ Word Break
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".
DP
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int len = s.size();
if(len <) return false;
if(dict.size() == ) return false;
vector<bool> flag(len+,false);
flag[] = true;
for(int i = ; i< len+;++i){
for(int j = i-; j>=;--j)
if(flag[j]==true && dict.find(s.substr(j,i-j))!= dict.end())
{
flag[i] = true;
break;
}
}
return flag[len];
}
};
LeetCode _ Word Break的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [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 ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- [Leetcode Week9]Word Break
Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...
- 【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#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
随机推荐
- python 2017.1.9
python对缩进和空格要求非常严格,要求非常对齐 print时不同字符串之间会自动加上空格 while 和 if 等没有结束标记,全部通过对齐的方式表示的
- CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元
题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...
- gzip优化网络传输量提高传输效率[转]
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;us ...
- iomanip
在c++程序里面经常见到下面的头文件 #include <iomanip> io代表输入输出,manip是manipulator(操纵器)的缩写(在c++上只能通过输入缩写才有效.) 2作 ...
- 用switch判断月份的练习
import java.util.Scanner; public class SwitchTest01 { public static void main(String[] args) { Syste ...
- el和jstl
<%@page import="cn.bdqn.bean.News"%> <%@ page language="java" import=&q ...
- css01入门小例子
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- IIS配置
IIS配置文档: 1.安装IIS.控制面板→程序→打开关闭Windows功能,Web管理服务和万维网服务都勾上. 2.部署网站:ASP.Net项目的发布:项目中点右键“发布”,选择“文件系统”,发布到 ...
- java使用内部类的好处及其初始化
java使用内部类的原因 每个内部类都能独立地继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响 java内部类初始化 ForeCatal ...
- phpMyAdmin配置及 错误 缺少 mysqli 扩展。请检查 PHP 配置
PHPMyadmin配置文件config.inc.php内容如下,在需要设置的地方增加了相关注释. 非常适合对数据库操作命令不熟悉的数据库管理者,下面我就说下怎么安装该工具: 1.先到网上下载 ...