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. [Unity] 查找资源

    有时候需要通过代码来为对象指定一个资源.可以通过下面的函数来查找资源. /// <summary> /// 查找资源 /// </summary> /// <return ...

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

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

  3. [CentOs7]图形界面

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

  4. Promise编程基础

    (一) Promise基础     所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消 ...

  5. Highcharts属性中英文参照

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. Properties类的使用方法

    它提供了几个主要的方法: 1. getProperty ( String key),用指定的键在此属性列表中搜索属性.也就是通过参数 key ,得到 key 所对应的 value. 2. load ( ...

  7. 移动端 css实现自适应正圆 ( 宽高随着手机屏幕宽度自适应 )

    序言:应朋友要求随手写了一下移动端 css实现自适应正圆 ( 宽高随着手机屏幕宽度自适应 ) ,以备后用 LESS代码: .adaptive-circle { margin: 50px auto 0; ...

  8. 日常使用的shell脚本

    1.shell实现无密码登陆 host=$ expect << EOF         spawn ssh-copy-id $host         expect "passw ...

  9. POJ 3292 Semi-prime H-numbers

    类似素数筛... Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6873 Accept ...

  10. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...