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

Note: All inputs will be in lower-case.

anagrams 的意思是两个词用相同的字母组成  比如 “dog" "god"

思路:

把单词排序 如 dog 按字母排序变为 dgo

用unordered_map<string, int> 记录排序后序列第一次出现时,字符串在输入string向量中的位置

用vector<bool> 记录每个输入字符串是否为anagram, 如果在map中发现已经存在了,就记录当前和初始的字符串都是anagram

class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ans;
vector<bool> isanagrams(strs.size(), false);
unordered_map<string, int> hash;
if(strs.size() == )
return ans; for(int i = ; i < strs.size(); i++)
{
string cur = strs[i];
sort(cur.begin(), cur.end());
if(hash.find(cur) == hash.end()) //没出现过
{
hash[cur] = i; //记录第一次出现是strs中的哪一个
}
else //出现过
{
isanagrams[hash[cur]] = true;
isanagrams[i] = true;
}
} for(int j = ; j < strs.size(); j++)
{
if(isanagrams[j] == true)
{
ans.push_back(strs[j]);
}
} return ans;
}
};

【leetcode】Anagrams (middle)的更多相关文章

  1. 【LeetCode】876. Middle of the Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...

  2. 【leetcode】Anagrams

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

  3. 【leetcode】Permutations (middle)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. 【leetcode】Combinations (middle)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. 【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 ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【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 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. 深度剖析:如何实现一个 Virtual DOM 算法

    本文转载自:https://github.com/livoras/blog/issues/13 目录: 1 前言 2 对前端应用状态管理思考 3 Virtual DOM 算法 4 算法实现 4.1 步 ...

  2. 11个Visual Studio代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  3. iOS分类、延展和子类的区别

    iOS分类.延展和子类的区别 类别.延展.子类的区别   类别 延展 子类 功能 为类添加方法,不用知道类的源码,添加变量(通过运行时,具体参考下面注解) 为类添加私有变量和私有方法,在类的源文件中书 ...

  4. HDU 1048

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char ...

  5. block 块函数

    定义模块函数: <?php function smarty_block_text($args,$content,$smarty,$a) { $color=$args["color&qu ...

  6. CSS使用自定义光标样式-遁地龙卷风

    测试环境是chrome浏览器 Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357. ...

  7. MongoDB 基础知识

    一. 基础知识 1. MongoDB是一个文档型的数据库,文档就是一个键值对的有序集合. 例如这样:{"greeting":"hello world"} 2. ...

  8. 他们在军训,我在搞 OI(Ending)

    Day 7 上午看看数学书,老师让我把导数相关的概念学了.这也没有多高大上,就是一坨公式需要背,什么 (a)' = 0 啦,什么 (xn)' = n·xn-1 啦,什么 sin'(x) = cos(x ...

  9. 最简单的Android教程之自定义控件

    新建title.xml,完成布局 新建一个TitleLayout继承 LinearLayout. activity_main.xml中引用 Run your applicaiton , and try ...

  10. ClearCanvas DICOM 开发系列 一

    概述 C#开源的DICOM server.支持影像处理.影像归档.影像管理.影像传输和影像浏览功能.开源代码可学习地方很多. 官方网站:http://www.clearcanvas.ca buildi ...