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. Emgu.CV 播放视频

    using Emgu.CV; using System; using System.Drawing; using System.Threading; using System.Windows.Form ...

  2. bowtie1和bowtie2的比较

    1.对于长度大于50bp的reads,bowtie2更精确:而小于50bp的reads,bowtie1更精确更快速 2.bowtie2支持的reads长度没有上限,当然reads长度在50~1000b ...

  3. PHP输出XML文件函数

    PHP输出XML文件函数 function xml_out($content, $charset = 'utf-8') { @header("Expires: -1"); @hea ...

  4. java基础 集合类

    java集合类主要有以下集中: List结构的集合类: ArrayList类, LinkedList类, Vector类, Stack类 Map结构的集合类: HashMap类,Hashtable类 ...

  5. 利用Shodan和Censys进行信息侦查

    在渗透测试的初始阶段,Shodan.Censys等在线资源可以作为一个起点来识别目标机构的技术痕迹.本文中就以二者提供的Python API为例,举例介绍如何使用它们进行渗透测试初期的信息侦查. Sh ...

  6. java26

    1:网络编程(理解)    (1)网络编程:用Java语言实现计算机间数据的信息传递和资源共享    (2)网络编程模型    (3)网络编程的三要素        A:IP地址           ...

  7. 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

    原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...

  8. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  9. des (C语言)

    /** * \file des.h * * \brief DES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...

  10. 如何创建 Code Snippet

    比如有一行自定义代码段: @property (nonatomic,copy) NSString *<#string#>; 需要添加到 Code Snippet 上,以帮助开发人员开发更便 ...