zoukankan      html  css  js  c++  java
  • LeetCode

    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-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;
    }

     

  • 相关阅读:
    农历查询
    C#颜色转换函数
    在IIS部署Silverlight网站
    silverlight双击事件处理
    关于List.Sort想到的
    sql获取总列数
    NHibernate的no persister for
    如何快速构建React组件库
    如何用canvas拍出 jDer's工作照
    Picker 组件的设计与实现
  • 原文地址:https://www.cnblogs.com/crazyacking/p/5255573.html
Copyright © 2011-2022 走看看