原题地址

Anagram:变位词。两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同

第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么。。后来还是看网上其他人的总结知道是怎么回事。

通常的做法是:把字符串内的字符排序,这样,凡是变位词都会变成相同的单词。用map记录这样的单词出现了几个,如果超过1个,则加入结果集中。

代码:

 vector<string> anagrams(vector<string> &strs) {
vector<string> res;
map<string, int> record; for (int i = ; i < strs.size(); i++) {
string tmp = strs[i];
sort(tmp.begin(), tmp.end());
map<string, int>::iterator it = record.find(tmp);
if (it == record.end())
record.insert(pair<string, int>(tmp, ));
else
it->second++;
} for (int i = ; i < strs.size(); i++) {
string tmp = strs[i];
sort(tmp.begin(), tmp.end());
map<string, int>::iterator it = record.find(tmp);
if (it->second > )
res.push_back(strs[i]);
} return res;
}

Leetcode#49 Anagrams的更多相关文章

  1. [Leetcode][Python]49: Anagrams

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 49: Anagramshttps://leetcode.com/proble ...

  2. LeetCode 49: 字母异位词分组 Group Anagrams

    LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...

  3. LeetCode - 49. Group Anagrams

    49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...

  4. leetcode@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

  5. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

  6. [LeetCode] 49. Group Anagrams 分组变位词

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  7. [leetcode]49. Group Anagrams变位词归类

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  8. 【LeetCode】49. Anagrams (2 solutions)

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

  9. (LeetCode 49)Anagrams

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

随机推荐

  1. AWS学习之EC2

    1.AWS简介 AWS(Amazon Web Services )提供了一整套云计算服务,让您能够构建复杂.可扩展的应用程序.如今,成千上万各种规模的客户都在使用这些云计算服务,它们涉及各个行业,包括 ...

  2. C++读入一个参数

    题目内容:已知正方形的边长,试编程求出其面积. 输入描述:输入不超过50个正整数的数据n(1<=n<=10000),每个正整数间以空格隔开. 输出描述:每次读入一个正整数,便输出其正方形的 ...

  3. ORM之Dapper操作Sql Server和MySql数据库

    1.为什么选择Dapper 1)轻量. 2)速度快.Dapper的速度接近与IDataReader,取列表的数据超过了DataTable. 3)支持多种数据库.Dapper可以在所有Ado.net P ...

  4. WCF中使用控件的委托,线程中的UI委托

    UI界面: <Window x:Class="InheritDemo.Window1" xmlns="http://schemas.microsoft.com/wi ...

  5. rails 学习笔记

    bundle package   #保存gem到 vendor/cache bundle install –local  从cache从安装 升级rails bundle config –delete ...

  6. Android线程---UI线程和非UI线程之间通信

        近期自学到了线程这一块,用了一上午的时间终于搞出来了主.子线程间的相互通信.当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message.我的主线程 ...

  7. Windows Phone Listbox虚拟化的问题

    最近在项目里面碰到最头疼的一个难题就是Listbox虚拟化的问题,查阅很多文档其实Listbox本身是支持虚拟化的,那么在什么情况下会破坏Listbox的虚拟化呢?目前我接触到的主要有两个原因:一.是 ...

  8. ZLG_GUI配置与函数介绍

    http://www.docin.com/p-825479457.html ZLG_GUI配置与函数介绍

  9. Go实现线程池

    本文通过代码讲解如何实现一个线程池.代码(来自https://gobyexample.com/)及注释如下: package main import "fmt" import &q ...

  10. Objective-C 一些概念

    Automatic Reference Counting (ARC)