Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

Summary: To sort and compare strings, using map to record distinct strings.

 class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> result;
map<string, int> str_map;
for(int i = ; i < strs.size(); i ++) {
string tmp = strs[i];
std::sort(tmp.begin(), tmp.end());
if(str_map.find(tmp) == str_map.end()) {
str_map[tmp] = i;
}else {
result.push_back(strs[i]);
if(str_map[tmp] >= ) {
result.push_back(strs[str_map[tmp]]);
str_map[tmp] = -;
}
}
}
return result;
}
};

Anagrams [LeetCode]的更多相关文章

  1. Group Anagrams - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...

  2. Anagrams leetcode java

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

  3. 49. Group Anagrams - LeetCode

    Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. [LeetCode] Find Anagram Mappings 寻找异构映射

    Given two lists A and B, and B is an anagram of A. B is an anagram of A means B is made by randomizi ...

  6. LeetCode 字符串专题(一)

    目录 LeetCode 字符串专题 <c++> \([5]\) Longest Palindromic Substring \([28]\) Implement strStr() [\(4 ...

  7. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  8. [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  9. [LeetCode] Anagrams 错位词

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

随机推荐

  1. ubuntu su 密码

    Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码. Ubuntu刚安装后,不能在terminal中运行su命令,因为root没有默认密码,需要手动设定.以安装ubuntu时输 ...

  2. ubuntu安装miniconda

    系统:ubuntu15.04   64位 wget -c http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh chm ...

  3. 最近用到这个强大的工具 PhysicsEditor (转)

    今天收到PhysicsEditor作者发过来的license key,所以顺便把PhysicsEditor也尝试了一下.主要是尝试将PhysicsEditor与cocos2dx,box2d结合开发的一 ...

  4. java获取中问名字的首字母

    public class FirstLetterUtil { private static int BEGIN = 45217; private static int END = 63486; // ...

  5. Java面向对象深度

    局部内部类 package ch6; /** * Created by Jiqing on 2016/11/21. */ public class LocalInnerClass { // 局部内部类 ...

  6. Docker-创建一个mysql容器,并保存为本地镜像

    查找docker hub上的镜像 [root@wls12c ~]$ docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysq ...

  7. z-score

    标准分数(standard score)也叫z分数(z-score),是一个分数与平均数的差再除以标准差的过程.用公式表示为: z=(x-μ)/σ.其中x为某一具体分数, μ为平均数,σ为标准差. Z ...

  8. laravel 开启sql查询日志

    \DB::enableQueryLog(); dd(\DB::getQueryLog());

  9. 使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录

    使用连接(JOIN)来代替子查询(Sub-Queries) MySQL从 4.1开始支持SQL的子查询.这个技术可以使用SELECT语句来创建一个单列的查询结果,然后把这个结果作为过滤条件用在另一个查 ...

  10. 转:c++类实例在内存中的分配

    转自:http://blog.csdn.net/alexwei2009/article/details/6157926 c++是一种面向对象的编程语言,它向下保持了对c的兼容,同时也允许程序员能够自由 ...