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. Maven实战(二)构建简单Maven项目

    上一节讲了maven的安装和配置,这一节我们来学习一下创建一个简单的Maven项目 1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: mvn archetype:gene ...

  2. win7出现无法连接到代理服务器的错误,不能上网的问题的解决

    今天晚上突然停电,等我打开电脑发现不然上网,用google浏览器出现这个错误: 用IE诊断错误如下: 说是不能连到代理服务器,但是我没有连接到代理服务器啊,但是我的QQ能登,就是不能用浏览器上网,经过 ...

  3. 將後台的Json數據返回到前台

    前台JS代碼 $.post('/Book/GetBookClassIDByName', { BookName: "旅遊手冊" }, function (data) { if (da ...

  4. KMeans的图像压缩

    # -*- coding: utf-8 -*- """ Created on Thu Aug 11 18:54:12 2016 @author: Administrato ...

  5. css设置中文字体(font-family:"黑体")后样式失效问题

    做项目时偶遇的一诡异问题,同样的代码,在ff和IE7以上页面显示正常,但IE6无论怎么改都不起作用,本来以为是IE6的某些浮动bug所致,结果弄了很长时间也不行,后来不经意间把原来设定的font-fa ...

  6. css绘制特殊图形基础

    1.等腰三角形 .isosceles{ width:; height:; border:30px solid; border-left-color: transparent; border-right ...

  7. {C#}{GDI+}各种C#,GDI+的资料

    GDI+各种功能: http://www.cnblogs.com/08shiyan/category/253906.html 字体:http://blog.sina.com.cn/s/blog_7c7 ...

  8. 根据Excel线程句柄得到ID并且关闭进程

    [System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropSe ...

  9. 循序渐进Python3(三) -- 0 -- 初识函数

    函数 如果我们要计算一个圆的面积,就需要知道它的半径,然后根据公式S=3.14*r*r算出它的面积,如果我们要算100个圆的面积,则每次我们都需要写公式去计算,是不是很麻烦,但是有了函数的话,我们就不 ...

  10. rotate the clock

    A program test: You are given N round clocks. Every clock has M hands, and these hands can point to ...