首页 > 其他 > 详细

leetcode - Anagrams

时间:2014-10-23 09:32:21      阅读:222      评论:0      收藏:0      [点我收藏+]

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

Note: All inputs will be in lower-case.

class Solution {
public:
    std::vector<std::string> anagrams(std::vector<std::string> &strs) {
        std::vector<std::string> res;
		std::map<std::string, int> ans;
		for (int i = 0; i < strs.size(); i++)
		{
			std::string s = strs[i];
			std::sort(s.begin(),s.end());
			if(ans.find(s) != ans.end())
			{
				if(ans[s] >= 0)
				{
					res.push_back(strs[ans[s]]);
					ans[s] = -1;
				}
				res.push_back(strs[i]);
			}
			else
			{
				ans.insert(std::make_pair(s,i));
			}
		}
		return res;
    }
};


leetcode - Anagrams

原文:http://blog.csdn.net/akibatakuya/article/details/40385589

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