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. 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 使用Entity Framework Code First模式构建数据库对象 已经决定使用EF C ...

  2. PHP系统声明式事务处理

    转自:http://www.jianshu.com/p/34261804bc45 1.数据库事务 事务(Transaction)是并发控制的基本单位.所谓的事务,它是一个操作序列,这些操作要么都执行, ...

  3. 再谈 $* 和 $@ 在 Bash 中的表现

    除非特别说明,本文中出现的 Shell 均指 Bash 4.3.首先说一个基础知识:Shell 中的变量在展开成值(Parameter Expansion)之后,这个值在某些上下文(Context)中 ...

  4. LYDSY模拟赛day2 Market

    /* orz claris,这个题的解法非常巧妙,首先是时间问题,其实这个问题只要离线处理一下就可以了,把物品和询问都按照时间排序,然后看一下能不能满足.然后,因为容量<=10^9,显然是不可能 ...

  5. cf723a The New Year: Meeting Friends

    There are three friend living on the straight line Ox in Lineland. The first friend lives at the poi ...

  6. Alpha版本十天冲刺——Day 1

    站立式会议 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 α版本接口文档初步编写,任务统筹 绘制燃尽图出错 学习http资源访问,服务器请求接口demo测试 作为PM,之前对团队具体要做 ...

  7. sqlserver实现数据库读写分离介绍

    对于负载均衡,笔者经常接触的当属Oracle的负载均衡机制.下面我们重点介绍Sql Server 2005是如何实现负载均衡的,感兴趣的朋友可以参考下哈 Internet的规模每一百天就会增长一倍,客 ...

  8. ICMP的应用--Traceroute

    Traceroute是用来侦测主机到目的主机之间所经路由情况的重要工具,也是最便利的工具.前面说到,尽管ping工具也可以进行侦测,但是,因为ip头的限制,ping不能完全的记录下所经过的路由器.所以 ...

  9. PHP学习-验证用户名密码

    登录页:login.php <?php //登录 if(!isset($_POST['submit'])){exit('非法访问!');} $username = $_POST['adname' ...

  10. JQuery实现图片轮播效果源码

    ======================整体结构======================== <div class="banner"> <ul class ...