题目:

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. javascritp日期函数总结

    getDate()与getDay()的区别: obj.getDate()返回一个月中的某一天:obj.getDay()返回一个星期中的某一天. getYear()与getFullYear()的区别: ...

  2. js各种宽高(1)

    在javascript中操作dom节点让其运动的时候,常常会涉及到各种宽高以及位置坐标等概念,如果不能很好地理解这些属性所代表的意义,就不能理解js的运动原理,同时,由于这些属性概念较多,加上浏览器之 ...

  3. 使用DBCP时发生AbstractMethodError异常

    使用DBCP时发生AbstractMethodError异常,错误描述: Exception in thread "main" java.lang.AbstractMethodEr ...

  4. java8新特性笔记

    1.forEach(),遍历数据结构中的元素,括号内可以带一个闭包的方法 2.双冒号用法:forEach(this::doSchedule),如果运行环境是闭包,java允许使用双冒号的写法来直接调用 ...

  5. mysql中的 IN和FIND_IN_SET的查询问题

    原来以为mysql可以进行这样的查询select id, list, name from table where 'daodao' IN (list);      (一)注:1. table含有三个字 ...

  6. android sdk国内目录http://mirrors.neusoft.edu.cn/android/repository/

    http://mirrors.neusoft.edu.cn/android/repository/

  7. Java 装箱 拆箱

    Java 自动装箱与拆箱   ??什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我 ...

  8. android JSON 技术

    json 语法检查和在线解析网址:http://json.parser.online.fr/ limengwe android Json解析详解(详细代码) http://blog.csdn.net/ ...

  9. gem

    bundle gem xxxrake build gem push pkg/xxx.gem rake releaserake install

  10. hdu 1427 速算24点

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...