题目:

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的更多相关文章

  1. 【Permutations】cpp

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  2. 【Subsets】cpp

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  3. 蓝桥杯 【dp?】.cpp

    题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...

  4. 【Triangle 】cpp

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  5. 【N-Queens】cpp

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  6. 【Combinations】cpp

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  7. 【Candy】cpp

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  8. 【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 = ...

  9. 【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 ...

随机推荐

  1. Eclipse 中打不开android sdk managerf

    今天配置android sdk 的时候,出现了android sdk 打不开的情况.无论直接点击 sdk manager.exe 还是从eclipse启动,都不起作用,双重启(重启eclipse和ad ...

  2. IOS基础——IOS学习路线图(一)

    一.一个月 1.OC语法基础. 2.KVC和KVO 3.IOS UI基础 4.UI表视图与集合视图 5.UIStoryboard和autoLayout 6.Ipad API 二.10天 7.静态页面考 ...

  3. c# 数据库操作学习

    一. 如何处理数据库连接 1. 数据库连接可以分为“物理连接”和“逻辑连接”(默认使用连接池的情况下Pooling=true): 物理连接:创建数据库连接时,默认会有一定数量的物理连接(默认Min P ...

  4. 如何消除选定TextBox后的光标但又不失去焦点。

    情景描述: 选择TextBox里的内容 Name:textTile 但是没有光标. 相关实现代码: [DllImport("user32", EntryPoint = " ...

  5. 基于jQuery编写的横向自适应幻灯片切换特效

    基于jQuery编写的横向自适应幻灯片切换特效 全屏自适应jquery焦点图切换特效,在IE6这个蛋疼的浏览器兼容性问题上得到了和谐,兼容IE6. 适用浏览器:IE6.IE7.IE8.360.Fire ...

  6. WordPress 主题开发 - (十三) Archive模板 待翻译

    What archive.php does (and all its related templates) is show posts based on a select criteria. A da ...

  7. 删:[CentOS 7] 安装nginx

    下载对应当前系统版本的nginx包(package) # wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...

  8. SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释

    SQLServer 2005 SELECT d.name N'TableName', d.xtype N'TableType', a.colorder N'ColumnIndex', a.name N ...

  9. C# 代码重启windows服务

    ServiceController service = new ServiceController("EnergyRecordService"); protected void b ...

  10. 《Prism 5.0源码走读》 设计模式

    Prism或Prism构建的应用程序时会使用大量的设计模式,本文简要列举Prism相关的那些设计模式. Adapter(适配器模式):Prism Library主要在Region和IoC contai ...