Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

Summary: To sort and compare strings, using map to record distinct strings.

 class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> result;
map<string, int> str_map;
for(int i = ; i < strs.size(); i ++) {
string tmp = strs[i];
std::sort(tmp.begin(), tmp.end());
if(str_map.find(tmp) == str_map.end()) {
str_map[tmp] = i;
}else {
result.push_back(strs[i]);
if(str_map[tmp] >= ) {
result.push_back(strs[str_map[tmp]]);
str_map[tmp] = -;
}
}
}
return result;
}
};

Anagrams [LeetCode]的更多相关文章

  1. Group Anagrams - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...

  2. Anagrams leetcode java

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  3. 49. Group Anagrams - LeetCode

    Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. [LeetCode] Find Anagram Mappings 寻找异构映射

    Given two lists A and B, and B is an anagram of A. B is an anagram of A means B is made by randomizi ...

  6. LeetCode 字符串专题(一)

    目录 LeetCode 字符串专题 <c++> \([5]\) Longest Palindromic Substring \([28]\) Implement strStr() [\(4 ...

  7. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  8. [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  9. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

随机推荐

  1. FreeSWITCH 1.6关于视频通话的一些测试

    简单的测试了一下,暂时没把精力放到这一块. ① 视频编码透传的设置(使用代理模式). 修改internal.xml文件的以下参数: <param name="inbound-proxy ...

  2. 基础!winForm客户端最常用的几个基本属性

    客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执行 WinForm常用窗体属性: 布 ...

  3. 列联表(Crosstabs)

    四格表(2*2的列联表): Tmin为最小的频数:N为频数之和. 1 当 Tmin≥5,N≥40时, 用普通卡方检验公式;2 当1≦Tmin≦5, N≥40时, 用校正卡方检验公式;3 Tmin< ...

  4. 用sql的select语句从数据库中获取数据

    基本的select语句 select语句中的算数表达式和NULL值 列的别名 使用连接符操作,literal character strings,alternative quote operator, ...

  5. 如何让div上下左右都居中

    在做登陆页面的话,需要login的div 上下左右都居中. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ...

  6. jsp get方式请求参数中包含中文乱码问题解决

    1. 自己接收到参数之后在后台进行转码处理 2: 修改tomcat的配置文件  server.xml <Connector port="8080" protocol=&quo ...

  7. JAVA中在Myeclipse里把表导入成相应的poco实体类

    参考:地址: http://blog.csdn.net/jintaiyong/article/details/7383982

  8. c 函数调用产生的汇编指令和数据在内存情况(1)

    一直对函数调用的具体汇编指令和各种变量在内存的具体分配,一知半解.各种资料都很详细,但是不实践,不亲自查看下内存总不能笃定.那就自己做下. 两个目的: 一,函数和函数调用编译后的汇编指令基本样貌 二, ...

  9. 半平面交模板(O(n*n)&& O(n*log(n))

    摘自http://blog.csdn.net/accry/article/details/6070621 首先解决问题:什么是半平面? 顾名思义,半平面就是指平面的一半,我们知道,一条直线可以将平面分 ...

  10. python paramiko模块SSH自动登录linux系统进行操作

    1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...