题目:

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. 文件上传ajaxfileupload.js插件

    Html:  <div class="container">         <form id="form" runat="serv ...

  2. POJ C程序设计进阶 编程题#4:括号匹配问题

    编程题#4:扩号匹配问题 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 在某 ...

  3. silverlight 退出当前页面、跳转到主页面

    1.退出当前页面 private void imgExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Message ...

  4. arguments .length .callee caller

    如果有一个函数像下面这样: function fn(){ } 那么fn这个函数下面就有一个arguments属性(你在逗我么,后面又说对象),该属性是个对象(typeof一下就知道了),然后它下面也有 ...

  5. RecyclerView的基本创建

    线性显示 类似于listview: 线性宫格显示 类似于grid view: 用线性宫格显示 类似于瀑布流: 结构图: 测试代码: activity_main.xml: <RelativeLay ...

  6. 解决Genymotion下载设备失败的方法(Connection Timeout)

    一直下载不下来,报错. 解决办法: 打开 C:\Users\用户名\AppData\Local\Genymobile目录 打开genymotion.log文件,在里面最下面几行,找到如下日志 [Deb ...

  7. 小课堂Week12 Clean Code Part1

    小课堂Week12 Clean Code Part1 今天的主题是函数,让我们看一个函数,找一找其中的"不整洁". 我们也根据这段代码,讨论下对于整洁代码的两个重要原则. publ ...

  8. 5.html5中的路径表示

    路径在html中的作用主要是进行外部资源的引入,如css文件,js文件,媒体文件等. 而路径本身有分为相对路径和绝对路径.所谓相对路径,就是相对于链接页面而言的另一个页面的路径.而绝对路径,就是直接从 ...

  9. [Prism框架实用分享]如何在主程序中合理的弹出子窗体

    大家好 说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件.不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件).最 ...

  10. [转]Oracle_ProC编程

    1.引言 由于PL/SQL不能用来开发面向普通用户的应用程序,必须借助其他语言或开发工具. 在Linux操作系统下应该用什么语言或开发工具来进行Oracle数据库应用的开发呢?本文将介绍2种方案:Pr ...