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. Quartz.Net 基于XML配置启动

    1.App.config <configSections> <section name="quartz" type="System.Configurat ...

  2. Java字节流:InputStream OutputStream

    字节输入流:InputStream 类声明: public abstract class InputStream implements Closeable 位于java.io包下,是一个抽象类. 官方 ...

  3. hdu4951 Multiplication table (乘法表的奥秘)

    http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...

  4. admin site

    基本步骤: 1.添加 'django.contrib.admin' 到 INSTALL_APP 设置中. 2.再添加四个依赖项: 'django.contrib.auth', 'django.cont ...

  5. web.xml配置解释

    web.xml中配置的加载优先级:首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的 ...

  6. [译]ngclass expressions in angularjs

    原文: http://blog.xebia.com/2014/01/31/ngclass-expressions-in-angularjs/ ngClass 指令允许你通过databinding一个表 ...

  7. PHP基础Mysql扩展库

    mysql扩展库操作步骤如下: 1.连接数据库 2.选择数据库 3.设置操作编码 4.发送指令sql,并返回结果集     ddl:数据定义语句     dml:数据操作语句     dql:数据查询 ...

  8. 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. 第四天 rxcocoa

    HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...

  10. 查看SQL Server日志 Part 1

    曾经有朋友问我数据被删除了,不借助第三方工具能不能查是什么时候发生的. SQL Server提供了一个undocumented的函数fn_dblog可以让我们查看活动的transaction log. ...