【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 ...
随机推荐
- [译]git log
git log git log命令用来显示提交的快照. 能列出来你项目的历史, 能过滤和搜索你指定的一些修改. git status能让你检查工作目录和stage区的状态, git log只提供被co ...
- EF-在EF中运行sql语句
DbRawSqlQuery<int> result2 = db.Database.SqlQuery<int>("SELECT count(*) FROM test.s ...
- C# 中excel操作
c#中设置Excel单元格格式 1.全表自动列宽 mysheet.Cells.Select(); mysheet.Cells.Columns.AutoFit(); 2.合并 excelRa ...
- LUXURY 8
A - Gargari and Bishops Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- java遍历map的四种方式
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 外国类似stackoverflow这样的网站访问慢怎么解决-遁地龙卷风
第二版 百度搜索蓝灯 下载桌面版 双击运行 如果打开的浏览器不是你想要的 拷贝地址栏地址给你想要的浏览器 一切就ok了!!!!! 建议不访问国外网站时,便将蓝灯关掉,否则在访问一些不开蓝灯能够正常访问 ...
- 关于cin,getchar(),scanf()的注意事项(转)
问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...
- iOS开发——项目篇—高仿百思不得姐 05——发布界面、发表文字界面、重识 bounds、frame、scrollView
加号界面(发布模块) 一.点击加号modal出发布模块,创建控件,布局控件1)使用xib加载view,如果在viewDidLoad创建控件并设置frame 那么self.view 的宽高 拿到的是xi ...
- BZOJ1012——[JSOI2008]最大数maxnumber
1.题目大意:求末尾L个数的最大值,强制在线 2.分析:这个拿线段树可以直接水过,然后我写了一个 维护单调栈, 二分求最大值的短代码,手懒.... #include <cstdio> #i ...
- unity3d教程游戏包含的一切文件导入资源
http://www.58player.com/blog-2327-954.html 导入资源 将文件增加至工程文件夹的资源 (Assets) 文件夹后,Unity 将自动检测文件.将任何资源 (As ...