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. redis+Keepalived实现Redis主从复制

    redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave:   10.10.10.204Virtural IP Addres ...

  2. Code First 关系 Fluent API

    通过实体框架 Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这意味着 Code First ...

  3. Java多线程编程核心技术---Java多线程技能

    基本概念 进程是操作系统结构的基础,是一次程序的执行,是一个程序及其数据结构在处理机上顺序执行时所发生的活动,是程序在一个数据集合上运行的过程,是系统进行资源分配和调度的独立单位.线程可以理解成是在进 ...

  4. 利用afxDump来调试自己的程序

    http://blog.csdn.net/sstower/article/details/7714199

  5. xcode的调试技巧

    转自:http://www.cnblogs.com/daiweilai/p/4421340.html#biyouji 目录 前言逼优鸡知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global ...

  6. WCF :IIS寄宿方式的Web地址、BaseAddress和EndPoint Address的关系

    对于在IIS中通过W3SVC或WAS寄宿的WCF Service,其在浏览器中显示的地址(Web地址),与其配置文件中的BaseAddress和EndPoint Address有什么关系呢?让我们来分 ...

  7. css的计数器

    更多关于计数器的问题可以参考:https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Getting_Started/Lists

  8. 【GXZ的原创】C++小游戏——五子棋

    前些时候考完试自己编的带有胜负判定的五子棋. 操作方法:WSAD或↑↓←→移动下棋位置,Space或Enter放置. 如果游戏出现bug,欢迎大家在评论区反馈. #include <stdio. ...

  9. CF #305(Div.2) D. Mike and Feet(数学推导)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  10. hdu.1043.Eight (打表 || 双广 + 奇偶逆序)

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...