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. 《深入理解Nginx》阅读与实践(三):使用upstream和subrequest访问第三方服务

    本文是对陶辉<深入理解Nginx>第5章内容的梳理以及实现,代码和注释基本出自此书. 一.upstream:以向nginx服务器的请求转化为向google服务器的搜索请求为例 (一)模块框 ...

  2. 【过程改进】10分钟进阶Nuget

    目录 nuget初识 nuget本地包 nuget解决依赖冲突 nuget是什么 .net版的maven(java)? 如果你用过windows的chocolatey,mac的homebrew或许更容 ...

  3. MySQL里的wait_timeout

    如果你没有修改过MySQL的配置,缺省情况下,wait_timeout的初始值是28800. wait_timeout过大有弊端,其体现就是MySQL里大量的SLEEP进程无法及时释放,拖累系统性能, ...

  4. python获取DBLP数据集

    #!/usr/bin/python # -*- coding: UTF-8 -*- import xml.sax import io, sys paper_tags = ('article', 'in ...

  5. flash builder4.7bug

    flash builder4.7项目,swc中的button实例出来有bug,解决办法: 1,把button都改成movieclip. 2,改用swf做资源.

  6. [转] 你是as3老鸟吗?但是有些你可能目前都不知道的东西

    你是as3老鸟吗?如果以下内容对你有莫大的帮助,请顶下! 一:加载swf库中的图片 new 的过程就是图片解压缩的过程.处于 Class 状态时,图片占用的内存和 SWF 文件中这个图片占用的磁盘空间 ...

  7. linux links and lynx

    接下来,说一下links 和 lynx 的一些基本操作,首先你,需要安装这俩个软件 yum install links yum install lynx links links的功能键 一些常见功能按 ...

  8. workerman 的属性

    <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Time: 16:35 */ use Workerma ...

  9. 纵表、横表互转的SQL

    纵表.横表互转的SQL By:大志若愚 1.建表: 纵表结构 Table_A  create table Table_A ( 姓名 ), 课程 ), 成绩 int ) ) ) ) ) ) 姓名 课程 ...

  10. ICMP

    (一)ICMP IP是一个尽力的不可靠的协议,IP不能提供差错控制(如果数据在传播过程中出现错误了),这个时候ICMP就起作用了. ICMP提供两个功能:差错的报告,查询. ICMP的ICMP包分为两 ...