Anagrams [LeetCode]
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
Summary: To sort and compare strings, using map to record distinct strings.
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> result;
map<string, int> str_map;
for(int i = ; i < strs.size(); i ++) {
string tmp = strs[i];
std::sort(tmp.begin(), tmp.end());
if(str_map.find(tmp) == str_map.end()) {
str_map[tmp] = i;
}else {
result.push_back(strs[i]);
if(str_map[tmp] >= ) {
result.push_back(strs[str_map[tmp]]);
str_map[tmp] = -;
}
}
}
return result;
}
};
Anagrams [LeetCode]的更多相关文章
- Group Anagrams - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...
- Anagrams leetcode java
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- [LeetCode] Find Anagram Mappings 寻找异构映射
Given two lists A and B, and B is an anagram of A. B is an anagram of A means B is made by randomizi ...
- LeetCode 字符串专题(一)
目录 LeetCode 字符串专题 <c++> \([5]\) Longest Palindromic Substring \([28]\) Implement strStr() [\(4 ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- DataTable或者DataRow转换对象
public static IEnumerable<T> ConvertObject<T>(DataTable dt) where T : new() { var v = ty ...
- C#中获取程序集版本号的方法
我的方法: string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 方法一: public v ...
- How To Use RUN_PRODUCT In Oracle Forms
Run_Product is used to run Oracle Reports (RDF/REP files) in Oracle Forms. It invokes one of the sup ...
- Servlet&jsp基础:第五部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- A Star算法笔记
回顾A*算法,偶得一源代码,略有瑕疵,改正之,并置于下. using System; using System.Collections.Generic; using System.Linq; usin ...
- IP地址匹配
问题描述: 在路由器中,一般来说转发模块采用最大前缀匹配原则进行目的端口查找,具体如下: IP地址和子网地址匹配: IP地址和子网地址所带掩码做AND运算后,得到的值与子网地址相同,则该IP地址与该子 ...
- 解耦HTML、CSS和JavaScript
当前在互联网上,任何一个稍微复杂的网站或者应用程序都会包含许多HTML.CSS和JavaScript.随着互联网运用的发展以及我们对它的依赖性日益增加,设定一个关于组织和维护你的前端代码的计划是绝对需 ...
- c trans
#define BUFSIZE 100 char buf[BUFSIZE]; ; int getch(void) { )? buf[--bufp] : getchar(); } void ungetc ...
- golang操作文件的四种方法
golang追加内容到文件末尾 字数349 阅读54 评论0 喜欢2 golang读写文件,网上很多教程了但是今天有个需求,想要把内容追加写到文件末尾google了好久,没有查到研究了一会儿file库 ...