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 * @ ...
随机推荐
- Spring MVC Web框架
1. Spring MVC简介 Spring MVC是java EE平台请求驱动类型的轻量级Web框架,使用了MVC设计模式的思想,spring框架的主要优势之一就是分层架构,分层架构允许选择使用 ...
- PostGreSQL存储过程
1 返回结果集的存储过程 -- drop FUNCTION getall();CREATE or REPLACE FUNCTION getall() RETURNS SETOF users AS$B ...
- hibernate多表查询,结果封装在自己定义的一个实体类当中(在自己定义的类中增加构造函数)
hibernate的hql查询直接返回java对象时出现问题3 向大家请教一个问题,现在有三张表,表之间没有关联,我需要将三张表里面的所有东西查询出来存储到一个新的对象中,该如何实现,使用hibern ...
- 怎样按xc或yc转正视图
extern void create_view(void) { tag_t wcs_id,matrix_id; double mtx[9],wcs_pt[3]; double x_axis[3]={1 ...
- Echarts动态加载后台数据
注意:1.用Ajax请求获取后台数据 2.Echarts只能处理Json数据 后台Controller:根据业务需求不同而返回不同数据,我前台要循环遍历Echarts的series进行数据添加,所以后 ...
- homework 15 2016 6 2 模板
#include<iostream>#include<cmath>#include<cstdio> using namespace std; template &l ...
- 关于MySql数据库设计表与查询耗时分析
本地建一张表persons,使用脚本插入了1000万条数据 下面比较几种查询方法的耗时(查询9000000到9000005这中间5条数据) 查询结果: 1: SELECT * FROM test.pe ...
- Nginx + tornado + supervisor部署
参考链接:supervisor + Tornado + Nginx 使用详解, 用tornado ,Supervisord ,nginx架网站, tornado官方文档 项目文档树: . ├── ch ...
- 【实战】初识ListView及提高效率
简介: ListView是手机上最常用的控件之一,几乎所有的程序都会用到,手机屏幕空间有限,当需要显示大量数据的时候,就需要借助ListView来实现,允许用户通过手指上下滑动的方式将屏幕外的数据滚动 ...
- SQL 修改列名
exec sp_rename 'student.Ssex','Sex','column'; 执行后就可达到目的. 即是运用了 exec sp_rename '表明.原列名','新列名','column ...