【Leetcode】【Medium】Group Anagrams
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:
- For the return value, each inner list's elements must follow the lexicographic order.
- 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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【LeetCode每天一题】Group Anagrams(变位词组)
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- 【转载】Fast Inserts to PostgreSQL with JDBC and COPY FROM
source: http://rostislav-matl.blogspot.com/2011/08/fast-inserts-to-postgresql-with-jdbc.html Thanks ...
- Objective c, +load, +initialize 方法
+load() 当类被加载入程序的时候会执行+load方法 +initialize() 当类第一次被使用的时候会执行+initialize方法 这两个方法都只会被执行一次.
- 种类并查集(POJ 1703)
1703 -- Find them, Catch them http://poj.org/problem?id=1703 题目大意:有2个敌对帮派,输入D a b表示a,b在不同帮派,输入A a b表 ...
- JavaScript valueOf() 函数详解
valueOf()函数用于返回指定对象的原始值. 该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法. 所有主流浏 ...
- (转载)iOS系统Crash文件分析方法
转自: http://ios-iphone.diandian.com/post/2012-05-18/19440182 Xcode 4.3的symbolicatecrash的位置和老版本的不一致了. ...
- expdp\impdp及exp\imp
数据泵文件 expdp介绍 EXPDP命令行选项1. ATTACH该选项用于在客户会话与已存在导出作用之间建立关联.语法如下ATTACH=[schema_name.]job_nameSchema_na ...
- SCI写作经验交流,别人的经验借鉴下,很有用的!
http://www.dxy.cn/bbs/topic/27127771 语言是非英语国家论文的最大障碍.首先是时态和语态:在前言和讨论里,描述该研究的过去历史和现状时,要使用相应的时态:过去就使用过 ...
- python第十二天-----RabbitMQ
有一位小伙伴说让我去A站写博客可能会有很多人喜欢,真是搞不懂,北方哪里有卖萌?北方默认状态就是这么萌的!再者说了,这明明就是很专注于技术的博客嘛,能不能严肃点!知不知道什么叫帧? 学习到了数据库的相关 ...
- Swift 为你的webView定制标题
有些情况下,应用中会使用webView来加载大段的文字,而且还是带各种标签的. 不能全部过滤掉,那样的话,内容就会失去原本想表达的格式. 可是,如果webView中并没有将内容的标题或其他杂项包含进那 ...
- Pig Latin
function translate(str) { //return str; var list = ['a','e','i','o','u']; if(list.indexOf(str[0]) &g ...