LeetCode
来源:互联网 发布:申请软件著作权 编辑:程序博客网 时间:2024/06/06 05:25
49. Group Anagrams Problem's Link
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-8);class Solution{public: vector<vector<string>> groupAnagrams(vector<string>& strs) { map<string,vector<string>> mp; for(int i=0;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=0;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 0;}
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- LeetCode
- LeetCode
- LeetCode
- LeetCode
- HTML5中新元素、新属性汇总;HTML5中已经不支持元素(或改为css实现)等
- LeetCode
- fedora
- NET第九章小型资源管理器
- 理解TCP/IP三次握手与四次挥手的正确姿势
- tcpdump详解
- JAVA
- 各大IT公司校园招聘程序猿笔试、面试题集锦
- hbase怎么查看一个region的大小
- 蓝桥杯最大的算式