题目如下:

Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.

分析如下:

之前遇到过类似的题目。处理的方法是,把每个词都sort一下,sort之后的结果作为key存入map中,比如ate在sort之后是aet,存入了map,下次再遇到一个词,如tea, 则sort之后发现map中已经存储过了,说明tea是一个anagram,遍历一遍输入,并且在map中查找当前word的key,只要map中存在当前word的key,就说明当前word是anagram。

这样处理之后,几乎把所有的anagram都找到了,但是第一次存入map中的word还漏掉了。所以再补上就行。

我的代码:

好不容易啊,在非IDE环境下写好一次提交bug free.不过时间运行时间挺长的,居然是332ms。

// 332msclass Solution {
public:vector<string> anagrams(vector<string> &strs) {vector<string> res;int len = (int)strs.size();if(len==0)return res;    map<string, string> string_map;map<string, string>::iterator string_map_it;map<string, int> count_map;map<string, int>::iterator count_map_it;for(int i=0;i<len;i++){string each_str=strs[i];sort(each_str.begin(),each_str.end());count_map_it=count_map.find(each_str);if(count_map_it==count_map.end()){count_map.insert(std::pair<string, int>(each_str,1));string_map.insert(std::pair<string, string>(each_str,strs[i])); //第一次出现的anagram先存入string_map中,稍后再补上。}else{count_map_it->second++;res.push_back(strs[i]); //当前word是anagram,所以放入输出的vector}}for(string_map_it=string_map.begin();string_map_it!=string_map.end();string_map_it++){ //补充第一次存入string_map的anagram到输出count_map_it=count_map.find(string_map_it->first);if( (count_map_it!=count_map.end() ) && ( (count_map_it->second)>1) )res.push_back(string_map_it->second);               }return res;}
};

改进分析:

上面的代码看着不太好的一点是,string_map存在的原因是,使得第一次存入key时,key对应的word不会被弄丢失。这样的开销比较大。看到这里有一个不错的答案。用mp[ss]=-1来做标记,防止第一次存入map的word被反复放入输出。

//208 ms
#include <set>
#include <map>
using namespace std;
class Solution {
public:vector<string> anagrams(vector<string> &strs) { //测试别人的一个更简洁的版本vector<string> res;map<string,int> mp;for (int i=0;i<strs.size();i++){string ss = strs[i];sort(ss.begin(),ss.end());if (mp.find(ss)!=mp.end()){res.push_back(strs[i]);if (mp[ss]!=-1){res.push_back(strs[mp[ss]]);    mp[ss]=-1;}}else{mp[ss]=i;}}return res;}
};

update: 2014 -11-28

新写的一版,也还算不是太罗嗦,虽然还是没有上面那版别人写得简洁。

// 228 ms
class Solution {
public:vector<string> anagrams(vector<string> &strs) {std::map<string, vector<string> > map1;std::map<string, vector<string> >::iterator it1;std::string current_string;vector<string> result;for (int i = 0; i < strs.size(); i++) {current_string = strs[i];std::sort(current_string.begin(), current_string.end() );map1[current_string].push_back(strs[i]);}for (it1 = map1.begin(); it1 != map1.end(); ++it1) {if (it1->second.size()>1)result.insert(result.end(), it1->second.begin(), it1->second.end());}return result;}
};

参考资料:

(1) Yu's Coding Garden

LeetCode(49)Anagram相关推荐

  1. leetcode 49. 字母异位词分组

    49. 字母异位词分组 - 力扣(LeetCode) 一开始想的是把各个字符串中每个字符ascii码值相加,然后相同的ascii码值放在一个组,在相同的ascii码值组中再去细找字母异位词. 一开始算 ...

  2. LeetCode 49字母异位词分组50pow(x,n)51八皇后

    原创公众号:bigsai 如果不错记得点赞收藏! 关注回复 bigsai 领取Java进阶pdf资源,回复进群加入力扣打卡群. 上周打卡内容:43字符串相乘&44通配符匹配 45跳跃游戏&am ...

  3. [LeetCode] Valid Anagram - 字符串排序比较系列

    题目概述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  4. LeetCode 49. 字母异位词分组(哈希)

    1. 题目 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例:输入: ["eat", "tea", "ta ...

  5. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分组中 ...

  6. [LeetCode] Valid Anagram

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  7. LeetCode 49 字母异位词分组

    力扣 思路:哈希表: 排序:对两个字符串分别进行排序之后得到的字符串是相同的 将排序之后的字符串作为哈希表的键     (参考LeetCode官方题解)  定义哈希表 unordered_map< ...

  8. 【Leetcode | 49】230. 二叉搜索树中第K小的元素

    给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = ...

  9. leetcode 49. 字母异位词分组(排序+hash)

    给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan&quo ...

最新文章

  1. python多线程端口扫描程序
  2. windows常见的运行命令以及各快捷键组合
  3. dijkstra算法matlab代码_头脑风暴优化(BSO)算法(附MATLAB代码)
  4. 回首向来萧瑟处,也无风雨也无晴~小祁的2018
  5. 云片短信php接口_php与阿里云短信接口接入
  6. 记录一次iOS11大标题不滚动的问题
  7. django 路由分发 url分层
  8. vue中v-show v-if v-bind的使用
  9. C# 在PPT中绘制图表——柱形图、环形图、混合型图表
  10. 天网防火墙Athena 2006正式发布
  11. 知识竞赛抢答器PLC设计
  12. MQTT——EMQX学习笔记03——java创建客户端,实现消息的发布与订阅
  13. seaweedfs学习
  14. d06调试详细说明_D06调试软件说明
  15. linux网易云打不开的问题
  16. 在 LaTeX 中插入图片
  17. 当你的职业是一名IT项目经理
  18. empty() 是 (boolean) var 的反义词 is_null() 是 is_set() 的反义词!
  19. fNIRS在发育科学中的应用
  20. games java mtech_JEIL MTECH打号机打码机

热门文章

  1. win10远程桌面连接计算机密码错误,win10远程桌面连接错误
  2. 食油大学C++程序设计题解
  3. data block address
  4. 关于计算机专业的调整与优化,基于oracle数据库系统性能调整与优化分析-计算机应用技术专业论文.docx...
  5. 【问题导向】利用R语言进行情感分析
  6. 高可用性GRE+IPSEC中心—分支
  7. matlab 画qpsk眼图,qpsk调制星座图:通信里 星座图 到底是什么意思
  8. 如何在IIS中配置https
  9. Broadcast的onReceive方法中弹出AlertDialog
  10. 7-6 哲哲打游戏 (25 分)