zoukankan      html  css  js  c++  java
  • LeetCode-Anagrams

    Given an array of strings, return all groups of strings that are anagrams.

    Note: All inputs will be in lower-case.

    目标是找出所有字母集合相同的词,例如aabc和abca

    通过hash就可以了

    class Solution {
    public:
        vector<string> anagrams(vector<string> &strs) {
            // Note: The Solution object is instantiated only once and is reused by each test case.
            map<vector<int>,vector<string> >ma;
           vector<int> vec;
           vector<string>ret;
           vec.resize(26);
           for(int i=0;i<strs.size();i++){
               for(int j=0;j<26;j++)vec[j]=0;
               for(int j=0;j<strs[i].length();j++){
                   vec[strs[i][j]-'a']++;
               }
               ma[vec].push_back(strs[i]);
           }
           for(map<vector<int>,vector<string> >::iterator it=ma.begin();it!=ma.end();it++){
               if((*it).second.size()>=2){
                   for(int i=0;i<(*it).second.size();i++){
                       ret.push_back((*it).second[i]);
                   }
               }
           }
           return ret;
        }
    };
    View Code
  • 相关阅读:
    [火柴排队]
    [NOI2001食物链]
    [黑科技]
    [SDOI2009HH的项链]
    [GXOI/GZOI2019旅行者]
    [Nim游戏]
    Log4Net
    C#创建windows服务并定时执行
    MySQL实现类似Oracle的序列
    DevExpress XtraTreeList的复选框 禁用
  • 原文地址:https://www.cnblogs.com/superzrx/p/3356004.html
Copyright © 2011-2022 走看看