【leetcode】Anagrams (middle)
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)的更多相关文章
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- 【leetcode】Anagrams
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- CSS这些代码你都不会,你还有什么好说的!!!
都说自己工资低的,先看看这些代码你能写出来不?这些都不会,你还嫌工资? 很多人抱怨工资低,觉得自己大材小用,但你真的是才不是柴嘛?? 经常听到一些人,遇到不会的问百度,如果这样,我们都不用学习了,天天 ...
- VS上利用C#实现一个简单的串口程序记录
一.背景 工作上需要利用串口往下位机写入数据,VC太老,正好借此机会来熟悉一直很想接触的VS之C#. 感谢Tony托尼哥的串口通信代码,感谢梦真的C#的技术支持. 二.正文 1.项目架构:(以我现有的 ...
- EF初接触01
自动属性:{get;set} 隐式类型 var, dynamic var: 隐式的类型推断出来,在编译阶段把Var换成对应的实际的类型 所以只应用在编译之间, 在运行阶段是和实际类型意义的 dyna ...
- ex6的选择器
前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuery的称赞,很多是由于jQuery方便的元素选择器 ...
- C++ 模板函数与模板类
一.模板函数 函数模板提供了一类函数的抽象,即代表了一类函数.当函数模板被实例化后,它会生成具体的模板函数.例如下面便是一个函数模板:
- python 深入模块和包
模块可以包含可执行语句以及函数的定义. 这些语句通常用于初始化模块. 它们只在 第一次 导入时执行.只在第一次导入的时候执行,第一次.妈蛋的第一次...后面再次导入就不执行了. [1](如果文件以脚本 ...
- mac 系统通用快捷键(mac 下的应用多数会往这些标准看齐)(转:http://yang3wei.github.io/blog/2013/02/08/chen-ni-yu-mac-chen-ni-yu-xcode/)
command + w: 关闭当前窗口 command + q: 退出程序 (Google Chrome 有点奇葩,按下之后还需要hold 那么一小会儿才能退出) command + m: 最小化当前 ...
- event相关
event.button 事件属性可返回一个整数,指示当事件被触发时哪个鼠标按键被点击. event.keyCode 事件属性可返回一个整数,指示当事件被触发时哪个键盘按键被点击. <scri ...
- MySQL分配角色权限
1.创建新用户 通过root用户登录之后创建 >> grant all privileges on *.* to testuser@localhost identified by &quo ...
- 中文的加密传输(python版)
信息传输过程中,可能会被各种监听. 这里介绍一种简单的加密算法(可逆). 正向加密: 字符串 -> 字节(char->int转换) -> 异或每个字节某个KEY ->字节(i ...