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. Bitcask 存储模型

    Bitcask 存储模型 Bitcask 是一个日志型.基于hash表结构的key-value存储模型,以Bitcask为存储模型的K-V系统有 Riak和 beansdb新版本. 日志型数据存储 何 ...

  2. JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  3. javascript函数命名的三种方式及区别

    1, function fn(val1,val2) { alert(val1+val2); } fn(1,2); 2, var fn=function() { alert(val1+val2); } ...

  4. CF460B Little Dima and Equation (水题?

    Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...

  5. [译]Mongoose指南 - Document

    更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...

  6. Unity3D 学习笔记

    不是什么技术文章,纯粹是我个人学习是遇到一些觉得需要注意的要点,当成笔记. 1.关于调试,在Android下无法断点,Debug也无法查看,查看日志方法可以启动adb的log功能,或者自己写个GUI控 ...

  7. 贝叶斯决策_bayes(新闻分类)

    1.简单例子引入 2.先验概率 3.后验概率 4.最小错误率决策 5.最小风险贝叶斯决策 1. 贝叶斯公式 2简单例子 正常情况下,我们可以快速的将街上的人分成男和女两类.这里街上的人就是我们观测到的 ...

  8. js实现自定义右键菜单--兼容IE、Firefox、Chrome

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  9. sass兼容IE8透明度方法

    你可以轻松的利用 {Sass::Script::Functions#ie_hex_str ie_hex_str} 函数对其做转换.$translucent-red: rgba(, , , 0.5); ...

  10. windowSoftInputMode属性讲解

    windowSoftInputMode属性讲解(下面这段内容我参考别人的博客,并加入我的一些意见) 我们从这个属性的名称中,可以很直观的看出它的作用,这个属性就是来设置窗口软键盘的交互模式的.andr ...