Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版
这次是词组字符串和匹配字符串相比较是否一致
请使用map来完成模式统计
class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char,int> mp;
map<string,int> ms;
vector<int> p,s;
int cnt = ;
for(string::size_type i = ; i < pattern.size(); ++i){
if(mp.find(pattern[i]) == mp.end()){
mp[pattern[i]] = cnt++;
p.push_back(mp[pattern[i]]);
}
else p.push_back(mp[pattern[i]]);
}
int a = , b = ;
cnt = ;
while((b = str.find(" ", a)) != string::npos){
string t = str.substr(a, b - a);
if(ms.find(t) == ms.end()){
ms[t] = cnt++;
s.push_back(ms[t]);
}
else s.push_back(ms[t]);
a = b + ;
}
b = str.size();
string t = str.substr(a, b - a);
if(ms.find(t) == ms.end()){
ms[t] = cnt++;
s.push_back(ms[t]);
}
else s.push_back(ms[t]);
if(p.size() != s.size()) return false;
for(vector<int>::size_type i = ; i < s.size(); ++i){
if(s[i] != p[i]) return false;
}
return true;
}
};
Leetcode 290 Word Pattern STL的更多相关文章
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- LeetCode 290. Word Pattern (词语模式)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- leetcode 290 Word Pattern(map的应用)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
随机推荐
- 《CSS3秘籍》(第三版)-读书笔记(4)
第12章 CSS页面布局 网页布局的类型: 固定宽度.不管浏览器窗口的宽度多大,网页内容的宽度始终保持不变. 流式.流式设计采用百分比,它会根据浏览器的宽度(无论有多宽)自动进行调整.网页会随着访问 ...
- sqlserver 分区表
我们知道很多事情都存在一个分治的思想,同样的道理我们也可以用到数据表上,当一个表很大很大的时候,我们就会想到将表拆 分成很多小表,查询的时候就到各个小表去查,最后进行汇总返回给调用方来加速我们的查询速 ...
- Win7中不能调试windows service
多年前玩过一次windows service,觉得挺简单的. 这次工作要维护产品中的windows service,发现不是那么简单,vs附加调试器的窗体中无法找到windows service进程. ...
- Android(Xamarin)之旅(一)
Xamarin废话我就不多说了. 就是一款编写Android和IOS应用的IDE,从Visual Studio2010就开始有个这个插件.只要发展什么的,我觉得在这里说还不如自己去百度呢. 入正题: ...
- oracle sql优化笔记
oracle优化一般分为:1.sql优化(现在oracle都会根据sql语句先进行必要的优化处理,这种应该用户不大了,但是像关联和嵌套查询肯定是和影响性能的) A.oracle的sql语句的条件是从右 ...
- Action类为何要继承ActionSupport
Action类为何要继承ActionSupport 理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.open ...
- Android音频系统之AudioFlinger(四)
http://blog.csdn.net/xuesen_lin/article/details/8805096 1.1.1 AudioMixer 每一个MixerThread都有一个唯一对应的Audi ...
- 【转】python fabric实现远程操作和部署
fabric title是开发,但是同时要干开发测试还有运维的活……为毛 task*3 不是 salary * 3 (o(╯□╰)o) 近期接手越来越多的东西,发布和运维的工作相当机械,加上频率还蛮高 ...
- JS实现隔行变色,鼠标移入高亮
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- iOS设置UISearchBar光标的颜色
[[UISearchBar appearance] setTintColor:[UIColor blackColor]];