【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 ...
随机推荐
- JavaScript Function(函数表达式)
创建函数 创建函数的方式有两种:1.函数声明,2.函数表达式 函数声明的语法为 functionName(); //不会报错,函数声明提升function functionName(arg0,arg1 ...
- A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war
完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...
- 关于网站高性能中磁盘cpu以及内存对网站性能的影响
之前和同事聊天的时候,提到了这个硬件方面(包括内存,cpu,以及硬盘的存储选择),个人认为可以从这几个方面来提高底层硬件的性能,从而提高网站的整体吞吐量和速度. 一.主机: (1).CPU:决定处理的 ...
- timingFunction
* 动画的开始与结束的快慢,有五个预置分别为(下同): * kCAMediaTimingFunctionLinear 线性,即匀速 * kCAMediaTimingFunc ...
- iOS的设备及分辨率、图片命名
iOS的设备及分辨率(iPhone竖屏/iPad横屏) 设备 分辨率 横宽比 iPhone 3GS.iPod Touch第三代 320 * 480 2 : 3 iPhone 4.iPod Touch第 ...
- 修改navigationbar右侧按钮离屏幕边缘位置
先上代码 UIButton *settingBtn = [Utils creatCustomButtonWithFrame:CGRectMake(, , , ) btnTitle: titleColo ...
- redis Transaction支持
前面主要介绍了redis数据类型,这里讲下事务问题 NoSQL都不支持事务,虽然Redis的Transactions提供的并不是严格的ACID的事务(比如一串用EXEC 提交执行的命令,在执行中服务器 ...
- 本地Mac如何搭建IPv6环境
http://blog.csdn.net/dancen/article/details/51595374 这篇文章写的很详细,借鉴用啦-
- vnc 登录后只有终端 没有桌面 黑屏
1, start vnc server: vncserver :1 issue: connect it with pc and only display one terminal. 2, stop v ...
- UE简单操作
一:UE恢复默认配置 1.开始--运行-- “%APPDATA%” 回车 . 2. 找到并打开 IDMComp 文件夹.将文件夹“UltraEdit”整个给删除. 3.重 ...