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

Note: All inputs will be in lower-case.

题目的意思是:给出一组字符串,按组返回拥有相同变位词的字符串

解题思路是:

  对单词中得字母排序,如果排序后的单词是一样的,那么我们可以判定这两个单词有相同的变位词。

  • 首先,求出每个单词的变位词,以变位词作为键插入哈希表中,值为一个链表。
  • 然后,把该单词附在这个链表末端。
  • 最后,遍历哈希表的值,输出长度大于1的字符串
class Solution {
public:
vector<string> anagrams(vector<string> &strs){
unordered_map<string, vector<int> > hash_map;
for(int i = ; i < strs.size(); ++ i){
string s = strs[i];
sort(s.begin(),s.end());
if(hash_map.find(s) != hash_map.end()){
vector<int> a = hash_map[s];
a.push_back(i);
hash_map[s] = a;
}else{
vector<int> a;
a.push_back(i);
hash_map.insert(make_pair(s,a));
}
}
vector<string> res;
for(unordered_map<string,vector<int> >::iterator iter = hash_map.begin(); iter!= hash_map.end();++iter){
vector<int> a = iter->second;
if(a.size() > ){
for(int i = ; i < a.size(); ++ i){
res.push_back(strs[a[i]]);
}
}
}
return res;
} };

Leetcode Anagrams的更多相关文章

  1. [LeetCode] Anagrams 错位词

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

  2. leetcode — anagrams

    import java.util.*; /** * * Source : https://oj.leetcode.com/problems/anagrams/ * * Created by lverp ...

  3. [leetcode]Anagrams @ Python

    原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of ...

  4. Leetcode: Anagrams(颠倒字母而成的字)

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

  5. LeetCode——Anagrams

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

  6. LeetCode ---Anagrams() 详解

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

  7. LeetCode Anagrams My solution

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

  8. LeetCode: Anagrams 解题报告

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

  9. [Leetcode] Anagrams 颠倒字母构成词

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

随机推荐

  1. Git入门

    转: http://www.cnblogs.com/luxiaojun/p/5944145.html

  2. 前端项目构建工具---Grunt

    什么是Grunt? grunt是javascript项目构建工具,在grunt流行之前,前端项目的构建打包大多数使用ant.(ant具体使用 可以google),但ant对于前端而言,存在不友好,执行 ...

  3. Bash 中的 _ 是不是环境变量

    首先,我们想到的会是 export(等价于 declare -x)命令: $ export | grep 'declare -x _=' 没有找到,那么结论就是 _ 不是环境变量?当然没那么简单,否则 ...

  4. UGUI 学习笔记

    1.UGUI中是没有depth的概念,那要怎么在脚本中动态的改变一个UI元素在hierarchy中的排序位置呢? 放到最上面 Transform.SetAsFirstSibling最下面Transfo ...

  5. Java与MySQL的连接

    下载数据库驱动文件,解压并保存至任意位置 下载地址 新建Java项目,并将驱动文件添加到项目中 项目名右键-->构建路径-->配置构建路径-->添加外部Jar 在项目中新建类,编写代 ...

  6. XHR——XMLHttpRquest对象

    创建XMLHttpRequest对象 与之前众多DOM操作一样,创建XHR对象也具有兼容性问题:IE6及之前的版本使用ActiveXObject,IE7之后及其它浏览器使用XMLHttpRequest ...

  7. redis cluster php 客户端 predis

    php有redis的扩展,目前来说,还不支持redis cluster,推荐一下predis,功能比较全,从单个,到主从,到cluster都是支持的.效率怎么样,要靠自己去测试一下. 1,下载pred ...

  8. PHP PHPUnit的简单使用

    1.window安装pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 2.在工作目录下,放两个文件: 1)C ...

  9. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  10. Linux运维(3年以内)

    1.精通shell编程,熟练应用awk,sed,grep,strace,tcpdump等常用命令; 2.精通windows server,linux,mssql,mysql,熟悉网络,cisco,ju ...