LeetCode - 49. Group Anagrams
49. Group Anagrams
Problem's Link
----------------------------------------------------------------------------
Mean:
给定一个由string类型构成的集合,让你按照每个字符串的单词构成集合来将这个集合分类.
analyse:
STL的运用.
Time complexity: O(N)
view code
/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-03-08-20.40
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-); class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
map<string,vector<string>> mp;
for(int i=;i<strs.size();++i)
{
string _str=strs[i];
sort(_str.begin(),_str.end());
mp[_str].push_back(strs[i]);
} vector<vector<string>> res;
for(auto mp_ptr:mp)
{
vector<string> _res;
for(auto _mp_ptr:mp_ptr.second)
{
_res.push_back(_mp_ptr);
}
sort(_res.begin(),_res.end());
res.push_back(_res);
}
return res;
}
}; int main()
{
int n;
while(cin>>n)
{
vector<string> strs(n);
for(int i=;i<n;++i)
cin>>strs[i];
Solution solution;
auto ans=solution.groupAnagrams(strs);
for(auto p1:ans)
{
for(auto p2:p1)
cout<<p2<<" ";
cout<<endl;
}
}
return ;
}
LeetCode - 49. Group Anagrams的更多相关文章
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- [LeetCode] 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. Example: Input: ["eat", "tea" ...
- LeetCode 49 Group Anagrams(字符串分组)
题目链接: https://leetcode.com/problems/anagrams/?tab=Description Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...
- [leetcode]49. Group Anagrams重排列字符串分组
是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupA ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- 刷题49. Group Anagrams
一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...
随机推荐
- Apache Thrift 跨语言服务开发框架
Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...
- openwrt-智能路由器hack技术(1)---"DNS劫持"
openwrt-智能路由器hack技术(1)---"DNS劫持" 1 导读 PS:之前写的一个文章,现在发现结构内容排版不是太好,导致阅读体验太差,影响传播和SEO,所以现在整 ...
- PPT嵌入字体的方法
使用ppt的时候,很多时候会使用一些特殊字体,在其他计算机上无法正常显示.这个时候就需要导出PPT的时候进行字体嵌入. 1.1 常规方法 所谓常规方法,是指那些字体的许可协议允许随意分发,我们才能导出 ...
- 开发者必知的几款App快速开发工具
“我有一个好创意,就差一个CTO……” ,这是今年炒的比较火的一句话. “原生APP开发难度大,周期长,成本高,还没上线市场已经被占领了.这个有没有解决方案?” “APP版本迭代更新,都是企业的一道难 ...
- var与this,{}与function 小记
JavaScript var是用来定义一个变量,this常用来定义或调用一个属性或方法.但是在全局范围内,this和var定义变量是等价的. window console.log('window:', ...
- Cassandra中的数据一致性
Cassandra中数据一致性指的是数据行在各个复制节点(replicas)上的更新和同步程度.通过提供tunable consistency,Cassandra扩展了eventual cons ...
- Perl重命名当前目录下的文件
下载的zip包解压后,中文都变成了乱码,就写了个脚本重命名文件. use strict; use warnings; use Cwd; sub main { my $dir = getcwd(); o ...
- 掌握js模板引擎
最近要做一个小项目,不管是使用angularjs还是reactjs,都觉得大材小用了.其实我可能只需要引入一个jquery,但想到jquery对dom的操作,对于早已习惯了双向绑定模式的我,何尝不是一 ...
- Java程序员的日常 —— 响应式导航Demo
这两天想要做响应式的页面,于是本着重复造轮子的想法,模仿Bootstrap官网,精简了一个响应式导航的Demo. 效果 代码 <!DOCTYPE html> <html> &l ...
- android TextView 文字垂直的设置
<TextView android:id="@+id/tv_status" android:layout_width="wrap_content" and ...