首页 > 其他 > 详细

Anagrams

时间:2015-06-29 20:27:58      阅读:148      评论: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://blog.csdn.net/leosha/article/details/46684499

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!