[leetcode 139]Word Break
2015-08-04 16:52
471 查看
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 =
dict =
Return true because
对从第一个字符到最后一个字符进行切分,新建一个数组备忘
count[i]=1表示字符串s从第一个字符到弟i个字符可以由字典组成
遍历字符串不断切分,返回count[s.size()]
AC代码
For example, given
s =
"leetcode",
dict =
["leet", "code"].
Return true because
"leetcode"can be segmented as
"leet code".
对从第一个字符到最后一个字符进行切分,新建一个数组备忘
count[i]=1表示字符串s从第一个字符到弟i个字符可以由字典组成
遍历字符串不断切分,返回count[s.size()]
AC代码
class Solution { public: bool wordBreak(string s, unordered_set<string>& wordDict) { int len=s.size(); int sum=wordDict.size(); if(len==0&&sum==0) return true; else if(len==0||sum==0) return false; int count[len+1]; memset(count,0,sizeof(count)); count[0]=1; string temp=""; for(int i=1;i<=len;++i) { for(int j=i-1;j>=0;--j) { temp=s.substr(j,i-j); if(count[j]&&wordDict.find(temp)!=wordDict.end()) { count[i]=1; break; } } } return count[len]; } };
其他Leetcode题目AC代码:https://github.com/PoughER/leetcode
相关文章推荐
- TCP/UDP的接收包方式
- Invalid proguard configuration file path D:\...\proguard.cfg does not exist or is not a regular fil
- 修复数据库表方法
- 2015-多校5-MZL's chemistry
- asp.net 发出xml请求和响应xml
- StartIO例程
- xcode6.3跟Xcode6.4插件失效解决方法
- SAP 币别设置
- Exchanging Partitions and Subpartitions with Tables--官方文档
- Algorithm Description
- Linq 模糊查询。Countains StartsWith EndsWith Indexof截取字符串,都可以用在Linq查询语句里
- 博文搬家
- Spark官方Blog:SequoiaDB与Spark深度整合
- Valid Anagram
- cocos2d-x 音效
- Berkeley's SICP in python3(五)
- sdk6下启用spidev
- SAP CO模块权限控制
- 运维必须掌握的Linux面试题
- C++ primer plus 练习10