【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 ...
随机推荐
- iOS原生的搜索:UISearchController
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...
- 提交上了,却在iTunes Connect没有新版本的任何消息
上架的时候,收到这样的邮件 This app attempts to access privacy-sensitive data without a usage description. The ap ...
- IDEA之maven(springmvc)项目
1.在idea下创建maven项目(参考IDEA之web项目(maven项目)创建) 2.项目结构 3.web.xml <!DOCTYPE web-app PUBLIC "-//Sun ...
- ORA-22868: 具有 LOB 的表包含有位于不同表空间的段
由于lob对象引起的表空间无法删除.本来是要删除DMS表空间,但是上面有LOB对象,而且表却是在别的表空间DMS4上.解决的办法就是将这些lob移动到DMS4表空间.下面是解决过程 删除用户时报错: ...
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- 使用 Intel HAXM 为 Android 模拟器加速,媲美真机
http://www.cnblogs.com/beginor/archive/2013/01/13/2858228.html
- Opencv CamShift+Kalman目标跟踪
#include "stdio.h" #include "string.h" #include "iostream" #include &q ...
- Android客户端的图形化拖放操作的设计实现
为什么要拖放?拖放在某些UI交互中可以简化用户操作. 拖放的步骤包括哪些?“Drag and Drop”,拖放,顾名思义,总共就分三步:1, 开始拖起来:2, 正在拖:3, 放下,进行操作:在这三步里 ...
- php搜索分页
最近做搜索分页的时候,发现第一页显示正常,点击到下一页的时候,显示结果变成了搜索全部内容. 仔细查看代码,发现当第一次输入关键词,提交到查询控制器的时候,表单提交的关键字不为空,可是点击到下一页的时候 ...