设为首页 加入收藏

TOP

★word_break--leetcode--动态规划
2015-07-20 17:46:28 来源: 作者: 【 】 浏览:3
Tags:word_break--leetcode-- 动态 规划
人人为我 递推型 动态规划:
class Solution {
public:
	bool wordBreak(string s, unordered_set
  
    &dict){
		int len = s.length();
		vector
   
     match(len + 1, false); match[0] = true; for (int i = 1; i <= len; i++){ for (int k = 0; k < i; k++){ match[i] = match[k] && (dict.find(s.substr(k, i - k)) != dict.end()); if (match[i]) break; } } return match[len]; } };
   
  
我为人人 递推型 动态规划:

class Solution {

public: bool wordBreak(string s, unordered_set &dict) { int len = s.length(); vector match(len + 1, false); match[0] = true; for (int i = 1; i <= len; ++i) { for (int j = i - 1; j >= 0; --j) { if (match[j]) { if (dict.find(s.substr(j, i - j)) != dict.end()) { match[i] = true; // 前i个字母可以match break; } } } } return match[len]; } };

最长上升字序列:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 3726 Graph and Queries trea.. 下一篇Binary Tree Preorder Traversal

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)