Leetcode Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
题目的意思是:给出一组字符串,按组返回拥有相同变位词的字符串
解题思路是:
对单词中得字母排序,如果排序后的单词是一样的,那么我们可以判定这两个单词有相同的变位词。
- 首先,求出每个单词的变位词,以变位词作为键插入哈希表中,值为一个链表。
- 然后,把该单词附在这个链表末端。
- 最后,遍历哈希表的值,输出长度大于1的字符串
class Solution {
public:
vector<string> anagrams(vector<string> &strs){
unordered_map<string, vector<int> > hash_map;
for(int i = ; i < strs.size(); ++ i){
string s = strs[i];
sort(s.begin(),s.end());
if(hash_map.find(s) != hash_map.end()){
vector<int> a = hash_map[s];
a.push_back(i);
hash_map[s] = a;
}else{
vector<int> a;
a.push_back(i);
hash_map.insert(make_pair(s,a));
}
}
vector<string> res;
for(unordered_map<string,vector<int> >::iterator iter = hash_map.begin(); iter!= hash_map.end();++iter){
vector<int> a = iter->second;
if(a.size() > ){
for(int i = ; i < a.size(); ++ i){
res.push_back(strs[a[i]]);
}
}
}
return res;
}
};
Leetcode Anagrams的更多相关文章
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- leetcode — anagrams
import java.util.*; /** * * Source : https://oj.leetcode.com/problems/anagrams/ * * Created by lverp ...
- [leetcode]Anagrams @ Python
原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of ...
- Leetcode: Anagrams(颠倒字母而成的字)
题目 Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- LeetCode——Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- LeetCode ---Anagrams() 详解
Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- LeetCode Anagrams My solution
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- LeetCode: Anagrams 解题报告
AnagramsGiven an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- [Leetcode] Anagrams 颠倒字母构成词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- 探索 OpenStack 之(8):Neutron 深入探索之 OVS + GRE 之 完整网络流程 篇
前两篇博文分别研究了Compute节点和Neutron节点内部的网络架构.本文通过一些典型流程案例来分析具体网络流程过程. 0. 环境 同 学习OpenStack之(7):Neutron 深入学习之 ...
- 精简高效的css命名准则
对于css,为了避免样式冲突,我们总会赋予相当特殊的命名,或是在选择符上添加html标记,或是使用层级.我们为避免在冲突上做文章,就会把代码的命名变得复杂化. 如果css的重用性越高,相比就越高效.如 ...
- Mac Sublime Text complie python .py error /bin/bash: shell_session_update: command not found
1.get the rvm version rvm -v 2.make sure the version at least 1.26 above. 3.then go ahead rvm get he ...
- ImageView的常用属性
ImageView的一些常用属性,并且这些属性都有与之对应的getter.setter方法: android:adjustViewBounds:设置ImageView是否调整自己的边界来保持所显示图片 ...
- Excel 锁定特定单元格 不允许更改
1.首先,excel默认所有单元格都是被"锁定"的--当"锁定单元格"按钮被激活时. 因此,第一步,选择所有单元格,设置单元格格式,取消"保护&quo ...
- Coursera系列-R Programming第二周
博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html --- 好久没发博客 且容我大吼一句 终于做完这周R Progra ...
- PHP正则表达式详解(二)
前言: 在本文中讲述了正则表达式中的组与向后引用,先前向后查看,条件测试,单词边界,选择符等表达式及例子,并分析了正则引擎在执行匹配时的内部机理. 本文是Jan Goyvaerts为RegexBudd ...
- 号外!GNOME 3.22 正式发布喽!!!
导读 经过半年的努力开发,别名为“卡尔斯鲁厄”的 GNOME 3.22 正式发布了!“GNOME Software 可以安装和更新 Flatpak 软件包,GNOME Builder 则可以创建它们, ...
- install alilang
$sudo dpkg -i alilang.deb $ sudo alilang
- JavaScript访问ab页面定时跳转代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...