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. ktouch移动端事件库

    最近闲来无事,写了个移动端的事件库,代码贴在下面,大家勿拍. /** @version 1.0.0 @author gangli @deprecated 移动端触摸事件库 */ (function ( ...

  2. [译]Probable C# 6.0 features illustrated

    原文: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated ========================= ...

  3. 一个简单的猜大小的小游戏 python

    初学python,用python写了一个简单的猜大小的小游戏 #!/usr/bin/env python #-*- coding:utf-8 -*- print "------------- ...

  4. PHP基础 mysqli的事务处理

    1: <?php 2: // PHP 的mysqli的事务处理 3: //======================================================== 4: ...

  5. Shanghai Regional Online Contest 1004

    Shanghai Regional Online Contest 1004 In the ACM International Collegiate Programming Contest, each ...

  6. 63.Hbase 常用命令

    1.进入 Hbase shell ./hbase shell 2. 命令 1.list 2.truncate 3.scan 4.describe 5.create 'tablename','famil ...

  7. Logistic 回归(sigmoid函数,手机的评价,梯度上升,批处理梯度,随机梯度,从疝气病症预测病马的死亡率

    (手机的颜色,大小,用户体验来加权统计总体的值)极大似然估计MLE 1.Logistic回归 Logistic regression (逻辑回归),是一种分类方法,用于二分类问题(即输出只有两种).如 ...

  8. 浮动层-JS兼容IE6

    //引用jquery 包/*orderBycat 与 orderBycatHead 双层浮动*/ $(window).scroll(function () { var top = $(window). ...

  9. 锋利的jQuery书中推荐的几款插件

    1.jQuery表单验证插件——Validation 2.jQuery表单插件——Form 3.模态窗口插件——SimpleModal 4.管理Cookie的插件——Cookie 5.jQuery U ...

  10. replace、replaceAll、replaceFirst的区别详解

    String s = "my.test.txt"; System.out.println(s.replace(".", "#")); Sys ...