原题地址

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. PHP isset()与empty()的区别详解

    通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言. 庞大的函数库支持着PHP语言功能的实现. 有关PHP函数isset()与empty()的相关用法. PHP的isset()函数 一 ...

  2. 对phpcms中{L('news')}的讲解

    直切话题 对于phpcms分M,C,A,那么现在要讲解的L是跟着M走的,每个M在languages中都有一个.lang.php文件,如Mcontent,就有一个content.lang.php,找到对 ...

  3. Laravel 5 基础(六)- 数据库迁移(Migrations)

    database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...

  4. WIN7 shutdown 定时/倒计时 命令关机

    解决方案: 一.可以通过DOS命令shutdown来解决 在 Win7 中,shutdown实现自动关机的方法如下: 开始->运行->cmd 运行"shutdown -s -t ...

  5. xml结构

    一.XmlHelper using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  6. Win10下IIS配置图解、MVC项目发布图解、IIS添加网站图解

    Win10下IIS配置 .找到控制面板:[开始]菜单鼠标右击,打开[控制面板] .打开控制面板,点击[程序],点击[启用或关闭Windows功能] 下一步,点击[启用虎关闭Windows功能] . 开 ...

  7. python zip函数介绍

    首先用help(zip)来看一下帮助文档:

  8. Ruby处理二进制(未完成)

    https://practicingruby.com/articles/binary-file-formats http://stackoverflow.com/questions/16821435/ ...

  9. 《Prism 5.0源码走读》 设计模式

    Prism或Prism构建的应用程序时会使用大量的设计模式,本文简要列举Prism相关的那些设计模式. Adapter(适配器模式):Prism Library主要在Region和IoC contai ...

  10. C语言--通用类型栈

    #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h&g ...