首页 > 其他 > 详细

[LeetCode] Anagrams

时间:2014-08-21 16:55:04      阅读:302      评论:0      收藏:0      [点我收藏+]

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

Note: All inputs will be in lower-case.

用map记录出现的string的下标,避免了再次遍历。

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        vector<string> result;
        int len = strs.size();
        map<string,int> strIndex;//key是string,value是对应在strs中的下标
        for(int i=0;i<len;i++){
            string s0 = strs[i];
            sort(s0.begin(),s0.end());
            if(strIndex.count(s0)==0)
                strIndex[s0] = i;
            else{
                if(find(result.begin(),result.end(),strs[strIndex[s0]])==result.end())
                    result.push_back(strs[strIndex[s0]]);
                result.push_back(strs[i]);
            }
        
        }//end for
        return result;
    }//end func
};

 

[LeetCode] Anagrams,布布扣,bubuko.com

[LeetCode] Anagrams

原文:http://www.cnblogs.com/Xylophone/p/3926954.html

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