49. Group Anagrams (string, HashTable)
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.
思路:
anagram: 由颠倒字母而成的单词
anagram无关乎字母出现的顺序,所以将字母排序后再比较每个字母出现的次数
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
if (strs.size() <= ) return result; vector<vector<string>> result;
map<string,int> anagram;
int resIndex = ;
string s; for (int i = ; i < strs.size(); ++i)
{
s = strs[i];
sort(s.begin(), s.end()); //anagram无关乎字母顺序,所以将string按字母排序后再比较
if (anagram.find(s) == anagram.end()) { //如果还没有出现过该anagram
vector<string> item;
item.push_back(strs[i]);
result.push_back(item);
anagram.insert(make_pair(s, resIndex++));
} else {
result[anagram[s]].push_back(strs[i]);
}
} //each inner list's elements must follow the lexicographic order
for(int i = ; i < result.size(); i++){
sort(result[i].begin(), result[i].end());
}
return result;
}
};
49. Group Anagrams (string, HashTable)的更多相关文章
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- 刷题49. Group Anagrams
一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- 49. Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 【LeetCode】49. Group Anagrams
题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode OJ 49. Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
随机推荐
- kill与kill -9的区别
有时候我们使用kill无法杀掉一个进程,但是用kill -9却可以,why? 首先看一下kill的功能,man手册对kill描述如下: KILL(1) Linux User's Manual KILL ...
- tensorflow :ckpt模型转换为pytorch : hdf5模型
参考链接:https://github.com/bermanmaxim/jaccardSegment/blob/master/ckpt_to_dd.py import tensorflow as tf ...
- Python PIL : IOError: decoder jpeg not available
The first thing I check when I got this error was to check if libjpeg was installed. Lets try this s ...
- centos中如何安装php-bcmath扩展?
talk is cheap,show me the code: [root@LAMP1 lib]# php -v PHP (cli) (built: Oct ::) Copyright (c) - T ...
- Linux下编写动态链接库
下面通过一个例子来介绍如何生成一个动态库.这里有一个头文件:so_test.h,三个.c文件:test_a.c.test_b.c.test_c.c,我们将这几个文件编译成一个动态库:libtest.s ...
- bzoj2957楼房重建
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树.每个点记录斜率,要一个单增的序列长度(从1开始). 线段树每个点记录自己区间的 ...
- LDAP基本概念
LDAP(Lightweight Directory Access Protocol)是一种基于计算模型的客户机/服务器X.500目录服务访问协议.LDAP是从X.500目录访问协议的基础上发展过来的 ...
- 【linux】linux文件属性权限的介绍
众所周知,root的信息存在/etc/passwd下,个人密码存在/etc/shadow下,所有组名存在/etc/group下,因此这三个文件十分重要. 在linux系统下,我们可以通过"l ...
- 谜一样的jquery之$选择器
jquery是一个强大的js类库,提供了很多便利的操作方法并兼容不同的浏览器,一旦使用便欲罢不能,根本停不下来,今天我们就来解读一下这个神秘的jquery源代码. 前几天思考再三,自己尝试着封装了一下 ...
- 黄聪:VS2010启动程序提示文件加载 使用 简体中文(GB2312)编码加载文件解决办法
vs2010 错误提示框:文件加载 使用 简体中文(GB2312)编码加载文件C:\Users\Administrator\AppData\Local\Temp\nxhgjasi.5au \Temp\ ...