[LeetCode]139.Word Break
来源:互联网 发布:哪个租房软件最靠谱 编辑:程序博客网 时间:2024/05/19 04:07
题目
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”.
思路
递归
代码
/*---------------------------------------* 日期:2015-05-07* 作者:SJF0115* 题目: 139.Word Break* 网址:https://leetcode.com/problems/word-break/* 结果:AC* 来源:LeetCode* 博客:-----------------------------------------*/#include <iostream>#include <vector>#include <unordered_set>using namespace std;class Solution {public: bool wordBreak(string s, unordered_set<string>& wordDict) { int size = wordDict.size(); if(size == 0){ return false; }//if vector<bool> visited(s.size(),false); return helper(s,wordDict,0,visited); }private: bool helper(string &s,unordered_set<string>& wordDict,int index,vector<bool> &visited){ if(index >= s.size()){ return true; }//if // 已经查看过表示行不通 if(visited[index]){ return false; }//if visited[index] = true; // 以index下标开始 for(int i = index;i < s.size();++i){ if(wordDict.find(s.substr(index,i - index + 1)) != wordDict.end()){ if(helper(s,wordDict,i+1,visited)){ return true; }//if }//if }//for return false; }};int main() { Solution solution; string str("leetcode"); unordered_set<string> wordDict = {"leet","co","code"}; cout<<solution.wordBreak(str,wordDict)<<endl;}
运行时间
0 0
- LeetCode 139. Word Break
- [LeetCode]139.Word Break
- [leetcode] 139.Word Break
- leetcode 139. Word Break
- [LeetCode]139. Word Break
- 139. Word Break LeetCode
- LeetCode-139.Word Break
- LeetCode *** 139. Word Break
- LeetCode 139. Word Break
- LeetCode 139. Word Break
- [leetcode] 139. Word Break
- [leetcode] 139. Word Break
- [LeetCode] 139. Word Break
- [leetcode]139. Word Break
- Leetcode-139. Word Break
- Leetcode-139. Word Break
- Leetcode 139. Word Break
- leetcode 139. Word Break
- 在Javascript 中创建JSON对象--例程分析
- Oracle与MySQL的几点区别
- Service优先级提高
- 最近的一点想法
- android Lollipop(5.0)--activity跳转动画
- [LeetCode]139.Word Break
- Html中CSS之去除li前面的小黑点,和ul、LI部分属性方法
- 排序查找算法实战---Median of Two Sorted Arrays
- Linux 下修改Tomcat使用的JVM内存大小
- 前端必读:浏览器内部工作原理
- ORA-01031: 权限不足
- Notepad++
- Linux网络编程——广播
- linux shell test manual