【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 ...
随机推荐
- asp.net webuploader粘贴,拖拽,点击上传图片
demo.html代码:<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset= ...
- SQL中补0
SQL中补0 编写人:CC阿爸 2014-3-14 第一种方法: right('00000'+cast(@count as varchar),5) 其中'00000'的个数为right函数的最后参数 ...
- mongodb 修改数据结构的一个例子以及小梳理
mongodb的存储结构是灵活可变的,但是,并不意味着我们就肆意地使用不规则的文档结构.不规则的文档结构对于开发和后期的维护都是一个灾难.所以,还是要有一个约定的格式. 但是,由于前期设计的不周详和其 ...
- a标签至于flash之上的时候,IE浏览器无法点击连接的问题
<a style="display: block;height: 131px;position: absolute;z-index: 999;width: 222px;backgrou ...
- 16)JAVA实现回调(Android,Swing中各类listener的实现)
熟悉MS-Windows和X Windows事件驱动设计模式的开发人员,通常是把一个方法的指针传递给事件源,当某一事件发生时来调用这个方法(也称为"回调").Java ...
- javascript arguments
此文为转载文章: 什么是arguments arguments 是是JavaScript里的一个内置对象,它很古怪,也经常被人所忽视,但实际上是很重要的.所有主要的js函数库都利用了arguments ...
- 实战Django:官方实例Part4
上一个part我们创建了投票的内容页,但这个页面仅仅局限于静态展示,投票的"投"字还无从体现.接下来,我们就来看一下,如何把票投起来. 19.创建表单 我们来更新模板文件pol ...
- delphi 博客地址收藏
博客地址: Delphi XE5 for Android系列,值得入门者一读http://www.cnblogs.com/ChinaEHR/p/3346867.html http://hi.baidu ...
- 【C#】 装箱 (boxing) 和拆箱 (unboxing)
目录: 1. 装箱和拆箱 2. 深入理解装箱和拆箱 3. int[] to object[],值类型数组到对象数组的转化 4. 使用泛型减少装箱和拆箱 1. 装箱和拆箱 装箱 就是把“值类型”转换成 ...
- JavaScript 组件化开发之路(一)
*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...