【Anagrams】 cpp
题目:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
代码:
class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
vector<string> ret;
map<string,vector<int> > str_indexs;
// trans strs to key (sorted string) and value (indexs of strs that have same key) pairs
for ( size_t i = ; i < strs.size(); ++i )
{
string key = strs[i];
std::sort(key.begin(), key.end());
str_indexs[key].push_back(i);
}
// add the keys which occurs more than once
for ( map<string,vector<int> >::iterator it = str_indexs.begin(); it!= str_indexs.end(); ++it )
{
if ( it->second.size()> )
{
for ( size_t k = ; k < it->second.size(); ++k )
{
ret.push_back(strs[it->second[k]]);
}
}
}
return ret;
}
};
tips:
对于std::map的使用还不太熟悉,接着这道题也熟悉一下。
这道题非常经典,直接搜的AC code。学习了一下,把看过的几个blog记录在下面。
http://bangbingsyb.blogspot.sg/2014/11/leetcode-anagrams.html
http://www.cnblogs.com/AnnieKim/archive/2013/04/25/3041982.html
================================================
第二次过这道题,忘记了算法了。这道题做法确实比较特殊。先把每个单词按照字典序进行排序,然后直接把单词作为hash table的key进行判断。
最后输出重复count大于1的字符。注意排序的时候,不要直接拿strs进行排序,否则没法返回结果了。
class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
map<string, vector<int> > string_count;
for ( int i=; i<strs.size(); ++i )
{
string tmp = strs[i];
std::sort(tmp.begin(), tmp.end());
string_count[tmp].push_back(i);
}
vector<string> ret;
for ( map<string, vector<int> >::iterator i=string_count.begin(); i!=string_count.end(); ++i )
{
if ( i->second.size()> )
{
for ( int j=; j<i->second.size(); ++j )
{
ret.push_back(strs[i->second[j]]);
}
}
}
return ret;
}
};
另外注意有个地方需要改进一下,最后往ret填充的时候,可以一个group一起insert了。
class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
map<string, vector<string> > string_count;
for ( int i=; i<strs.size(); ++i )
{
string tmp = strs[i];
std::sort(tmp.begin(), tmp.end());
string_count[tmp].push_back(strs[i]);
}
vector<string> ret;
for ( map<string, vector<string> >::iterator i=string_count.begin(); i!=string_count.end(); ++i )
{
if ( i->second.size()> )
{
ret.insert(ret.end(), i->second.begin(), i->second.end());
}
}
return ret;
}
};
【Anagrams】 cpp的更多相关文章
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- 【Subsets】cpp
题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 【Candy】cpp
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 【4Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 【3Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
随机推荐
- iOS初学者的AppStore上架应用"菜谱大师"开源了!
本人是一名DoNet程序猿,在业余的时间自学了点iOS,于是就自己弄了一个小菜谱,自己要做菜的时候也就可以用自己的菜谱了. 现在将此应用开源给像我一样对iOS开发有兴趣,并且想学习iOS的园友,毕竟这 ...
- 01-实现图片按钮的缩放、动画效果(block的初步应用)
#import "ViewController.h" #define kDelta 60 @interface ViewController () @end @implementa ...
- 【转】8种Nosql数据库系统对比
导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型nosql数据库的文章.文章由敏捷翻译 – 唐尤华编译.如需转载,请参见文后声明. 虽然SQL数据库是非常有 ...
- php中的一些小细节(1)
<?php $a=false; echo $a; //var_dump($a); ?> 输出结果为: (即为空): 为什么会这样子? 查看官网对echo的相关资料得出: 结论:ec ...
- Oracle获取表结构信息:表名、是否视图、字段名、类型、长度、非空、主键
select a.TABLE_NAME as "TableName", then 'V' else 'U'end as "TableType", a.COLUM ...
- 一些关于python的小感想
python是一门优秀的语言,但随之而来的是大量的知识,各种模块,相信一个人的大脑是很难记住如此多的内容.这时后的我们就应该想办法避免去记忆这么多的内容. 1.查看官方文档(英语很重要,啥也不说了) ...
- [terry笔记]RMAN综合学习之备份
rman是最经济实惠的oracle备份工具,在这里做一个rman的整体学习. 文章中大多是rman命令的语法,还是最好做做实验,以便印象深刻,因为大多数数据库的备份就是按时跑脚本,恢复也不是经常能遇到 ...
- 一个朋友js图表开发遇到的问题 解决思路c和js
引言 不求知道一切, 只求发现一件 -- 乔治·西蒙·欧姆 附注:那些存在于梦幻中的事迹,那些儿时梦中的人物,每每看起,都觉得 .哎 .... 岁月 ... 一直在努力 ... ...
- C,C++容易被忽略的问题
1.字符串数组,字符串指针可以直接输出 ]="I am a student"; cout<<s2<<endl; char *p="I am a s ...
- Python数据类型-----数字&字符串
Python数字类型 int类型表示的范围:-2147483648至2147483648之间,超出这个范围的数字即视为long(长整形) 在Python中不需要事先声明数据类型,它是根据具体的赋值来进 ...