Anagrams

问题描写叙述:
Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.
比如:输入为:{abc,bca,123,321,567}
输出为:{abc,bca,123,321}
解决方式:

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        int len = strs.size();
        map<string,int> map1;
        vector<string> result;
        for(int i = 0; i< len;i++)
        {
            string temp = strs[i];
            sort(temp.begin(),temp.end());
            if(map1.count(temp)){
                if(map1[temp]>=0)
                {
                    result.push_back(strs[map1[temp]]);
                    map1[temp] = -1;
                    result.push_back(strs[i]);
                }else
                {
                    result.push_back(strs[i]);
                }
            }else{
                map1[temp] = i;
            }

        }
        return result;
    }
};
posted @ 2016-01-13 18:41  mengfanrong  阅读(136)  评论(0编辑  收藏  举报