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.

    思考:map应用。遍历两次strs,第一次建立map,第二次打印符合提议的字符串。

    class Solution {
    private:
        vector<string> res;
        map<string,int> m;
    public:
        vector<string> anagrams(vector<string> &strs) {
            int i;
            for(i=0;i<strs.size();i++)
            {
                string temp=strs[i];
                sort(temp.begin(),temp.end());
                m[temp]++;
            }
            for(i=0;i<strs.size();i++)
            {
                string temp=strs[i];
                sort(temp.begin(),temp.end());
                map<string,int>::iterator iter=m.find(temp);
                if(iter!=m.end()&&iter->second>1)
                {
                    res.push_back(strs[i]);
                }
            }
            return res;
        }
    };
    

      

  • 相关阅读:
    sqli29-32环境搭建(winserver)
    sqli-labs(Basic)
    SQL语句
    8月10号
    8月9号
    第五周进度报告
    8月8号
    8月7号
    8月6号
    大道至简读后感
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3514971.html
Copyright © 2011-2022 走看看