Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]

Note:

  1. For the return value, each inner list's elements must follow the lexicographic order.
  2. All inputs will be in lower-case.

解题思路:

1、对原字符串进行排序,这样所有具有相同字符的字符串就得到了相同的结果。

2、使用hash表,key是排序后的字符串,value设置为结果数组中保存的地址(下标),这样,找打一个重复字符串就可以直接放入结果数组中。

注意:

1、返回结果中,对于相同字符的字符串,要按照字典序进行排序,因此还要多一步排序过程。

解题步骤:

1、新建结果数组和hash表;

2、遍历输入的原始字符串集合,对每个字符串:

  (1)排序

  (2)在hash中查找,如果找到,则取出value,对应到结果数组中,将原字符串插入;

  (3)如果在hash表中没找到,则:

    a. 先在结果二维数组中开辟新的一维数组,用来保存这个新字符组合;

    b. 插入当前这个字符串;

    c.在hash表中添加记录;value值是开辟的新数组位置;

3、最后遍历结果数组,对每个字数组进行排序;

AC代码:

 class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > ans;
unordered_map<string, int> mp; for (int i = ; i < strs.size(); ++i) {
string cur(strs[i]);
sort(cur.begin(), cur.end());
if (mp.find(cur) != mp.end()) {
ans[mp[cur]].push_back(strs[i]);
} else {
vector<string> tmp(, strs[i]);
ans.push_back(tmp);
mp[cur] = ans.size() - ;
}
} for (int i = ; i < ans.size(); ++i) {
sort(ans[i].begin(), ans[i].end());
} return ans;
}
};

【Leetcode】【Medium】Group Anagrams的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Group Anagrams(变位词组)

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. 【leetcode刷题笔记】Anagrams

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

  7. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  10. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. 移动端自动化环境搭建-Android-SDK的安装

    安装android的sdk包 A.安装依赖 我们做的是移动端的自动化测试,肯定就需要android的开发环境 网上也有好多教程,我只是用的最简单的 B.安装过程 首先需要前往android官网,找到S ...

  2. Salt官方将RHEL5/CentOS5 源

    Salt官方将RHEL5/CentOS5的软件包维护迁移到了Fedora Corp (https://copr.fedoraproject.org/coprs/saltstack/salt-el5/) ...

  3. Form文件夹开发步骤

    1.开发完一个Form,测试OK 1.添加Object Groups 操作如下:在同一窗口打开标准的Form APPSTAND.FMB 和我们自己客制的Form,并且选择APPSTAND.FMB的Ob ...

  4. mgo中DBRef-数据添加测试

    2014-1-25 在设计mongo数据库时遇到这样一个问题,日志信息表需要引用人员信息表的数据.如果是结构化数据库,基本上不用想太多的东西.由于刚接触非结构化数据库,按着书上的理解由于日志数量较多, ...

  5. java并发编程_CountDownLanch(倒计数锁存器)应用场景

    使用介绍: 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在 ...

  6. druid的安装

    最近想玩druid.druid的底层是fastbit索引的列式存储.采用分布式的zookeeper调度.实时大数据分析软件.主要针对OLAP操作. 搭环境搭环境.druid的核心成员成立了一个叫imp ...

  7. python生成中文验证码,带旋转,带干扰噪音线段

    # -*- coding: utf-8 -*- """ Created on Sun Oct 4 15:57:46 2015 @author: keithguofan & ...

  8. SQL Server SELECT逻辑处理顺序

    SQL Server SELECT语句,逻辑处理顺序,虽然SELECT位于语句最前面,它在逻辑处理中,基本上是最后一个被执行的部分. 下面列出查询子句在逻辑上处理顺序: 1.  FROM 2.  WH ...

  9. Howto: 如何将ArcGIS Server缓存移动到新服务器

     Howto: 如何将ArcGIS Server缓存移动到新服务器 文章编号: 33686 软件: ArcGIS Server 9.2, 9.3, 9.3.1 操作系统: Windows 2000, ...

  10. VC++ AfxBeginThread 与 CreateThread 的区别

    简言之:AfxBeginThread是MFC的全局函数,是对CreateThread的封装.    CreateThread是Win32 API函数,前者最终要调到后者.具体说来,CreateThre ...