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 * @ ...
随机推荐
- [转]Excel 取汉字拼音首位
转自:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 转自:http://jingyan.baidu.com/articl ...
- hadoop中master免登录slave
hadoop集群免登录配置 在主机master上执行如下: 1. $cd ~/.ssh(如果没有此目录,可以手动创建) 2. $ssh-keygen -t rsa ----------------- ...
- 设置EditText光标位置
editext.setSelection(int index);
- 华为手机打开Logcat的方法
华为手机默认是关闭logcat信息的,这在开发调试时当然很不方便,打开log信息的方法如下 1. 进入拨号界面输入:*#*#2846579#*#* 2. 依次选择ProjectMenu---后台设置 ...
- linux下挂载新硬盘
挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 1.创建新硬盘分区 用fdisk + 路径 进行分区 进入磁盘,对磁盘进行分区 #fdisk /dev/sdb Command (m for h ...
- easyui combotree 默认 初始化时就选中
做得权限管理系统,用combotree来控制权限,combotree是通过后台json来初始化,但是前台不是选中状态. 下面是核心代码已经标红. $('#txtTree').combotree({ m ...
- 万万没想到,3D打印居然可以做这些逆天设计
3D打印一直被冠以“高科技”头衔,似乎离我们的日常生活还很遥远.其实不然,随着技术的创新,3D打印技术逐渐深入各个领域,工业生产.商业.医学.建筑.艺术等领域都能看到3D打印技术的影子.它将会改变我们 ...
- 进程调度算法Linux进程调度算法
这次介绍一下操作系统的进程调度算法 操作系统的调度分为三种:1.远程调度(创建新进程):2.中程调度(交换功能的一部分):3.短程调度(下次执行哪个进程) 这次讲述的就是短程调度,可以简单的看作咱们平 ...
- NOIP2012 题解
Vigenère 密码 这个名字实在打不来... 题解:模拟 #include <cstdio> #include <cstring> +; bool cj; int cl, ...
- DBHelper
DBHelper: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...