Anagrams

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

Note: All inputs will be in lower-case.

 
 
Anagrams:即字母个数和字母都相同,但是字母顺序不相同的词
 
e.g. "tea","and","ate","eat","dan".   return "and","dan","tea","ate","eat"
 
即找出vector中所有存在Anagrams的词
 
思路:
对每一个string中的字符进行重新排序,保存在map中
 
如果重复找到了某个元素,则说明该词是Anagrams的,把当前元素,和与之匹配的Anagrams元素保存到vector中
 
注意sort的使用
 
如果比较的元素可以用<号比较,则无需第三个参数,按照升序排列
如果比较的元素不可以用<号比较,则需要第三个参数比较函数
比较函数可以写成下面的形式:
static bool less_lower(char c1, char c2)
{
    return c1<c2;
}
注意,对于compare函数,两个元素相等时,需要返回false,否则会报错
 
 class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
int n=strs.size();
string s;
map<string ,int> strMap;
vector<string> result; for(int i=;i<n;i++)
{
s=strs[i];
sort(s.begin(),s.end());
if(strMap.find(s)==strMap.end())
{
strMap[s]=i;
}
else
{
if(strMap[s]!=-)
{
result.push_back(strs[strMap[s]]);
strMap[s]=-;
}
result.push_back(strs[i]);
}
}
return result;
}
};

【leetcode】Anagrams的更多相关文章

  1. 【leetcode】Anagrams (middle)

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

  2. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. 在Razor中如何引入命名空间?("import namespace in razor view") 【转】

    原文链接 找了半天,原来如此: 在aspx中: <%@ Import Namespace = "Martian.Areas.SFC.Models" %><%@ I ...

  2. monit 监控并自动重启服务

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://coolerfeng.blog.51cto.com/133059/50126 Mo ...

  3. MQ介绍

    MQ    MQ传递主干, 在世界屡获殊荣. 它帮您搭建企业服务总线(ESB)的基础传输层.IBM WebSphere MQ为SOA提供可靠的消息传递.它为经过验证的消息传递主干, 全方位. 多用途的 ...

  4. [CentOs7]图形界面

    摘要 为了更方面的看到命令的执行后的效果,感觉安装一个图形界面,学习起来更有感觉.至少知道自己做了哪些事.在刚开始安装虚机的时候,选择了最小安装centos7,发现在使用命令安装图形界面的时候,尝试了 ...

  5. ie8及其以下浏览器的document.getElementsByClassName兼容性问题

    使用JavaScript访问DOM的一个重大问题是,此过程需要一种通过元素类名称来选择类的类函数,对DOMContentReady,这种类函数的缺失导致开发人员需要自己编写自定义脚本业执行上述任务,许 ...

  6. SQLServer安装错误之--->无法打开项 UNKNOWN\Components\DA42BC89BF25F5BD0AF18C3B9B1A1EE8\c1c4f01781cc94c4c8fb1542c0981a2a

    – 错误 1402.无法打开项 UNKNOWN\Components\7ABFE44842C12B390AF18C3B9B1A1EE8\54D21D49F3A8C1C49AC11A1B6445A83E ...

  7. [译]Profile and debug your ASP.NET MVC app with Glimpse

    原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...

  8. 安装cocoods

    http://www.tuicool.com/articles/7VvuAr3 http://blog.csdn.net/gf771115/article/details/50403253(详细,用终 ...

  9. ML—随机森林·1

    Introduction to Random forest(Simplified) With increase in computational power, we can now choose al ...

  10. 6.3.28微信需群主确认才可进群&发GIF动图功能内测开始了

    昨天下午有网友收到微信6.3.28新版内测邀请,不过这个内部体验目前貌似只对安卓手机开放,苹果的IOS系统还不支持,会提示“你当前使用的是非安卓设备,不建议下载安卓体验包,但你仍可邀请朋友尝鲜”.最新 ...