码迷,mamicode.com
首页 > 其他好文 > 详细

Anagrams

时间:2016-01-13 19:41:30      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

问题描写叙述:
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;
    }
};

Anagrams

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5128164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!