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]的更多相关文章

  1. Group Anagrams - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...

  2. Anagrams leetcode java

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  3. 49. Group Anagrams - LeetCode

    Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. [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 ...

  6. LeetCode 字符串专题(一)

    目录 LeetCode 字符串专题 <c++> \([5]\) Longest Palindromic Substring \([28]\) Implement strStr() [\(4 ...

  7. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  8. [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 ...

  9. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

随机推荐

  1. SQL 汉字转换成拼音首字母 首字母查

    -- ============================================= -- 功能:汉字转换成拼音首字母 首字母查 -- ========================== ...

  2. An Example of On-Error Trigger in Oracle Forms

    I wrote this trigger around 4 years ago to handle errors in an application based on Oracle Forms 6i. ...

  3. R语言画图基础参数设置

    Graphical Parameters You can customize many features of your graphs (fonts, colors, axes, titles) th ...

  4. oracle小测试

    1.请解释以下几个函数意思:NVL.TRUNC.DECODE.TO_DTAE.TO_CHAR.CONVERT.ROUND.TRIM.ISNULL.SUBSTR.GROUP BY.HAVING 答: N ...

  5. 屏幕序列Screen Sequences

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. CUBRID学习笔记 6 修改用户密码

    修改密码 可以在web管理中修改. 还有另外两种体位 1  语句 ALTER USER user_name PASSWORD 'any_password_here_in_single_quotes'; ...

  7. NSKeyedArchive(存储自定义对象)

    在viewController.m中: - (void)viewDidLoad { [super viewDidLoad]; ZWPerson *p = [[ZWPerson alloc] init] ...

  8. Linux命令工具基础02 文件及目录管理

    文件及目录管理 文件管理不外乎文件或目录的创建.删除.查询.移动,有mkdir/rm/mv 文件查询是重点,用find来进行查询:find的参数丰富,也非常强大: 查看文件内容是个大的话题,文本的处理 ...

  9. TCP三次握手及四次挥手详细 转

    一.TCP报文格式        TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图: 图1 TCP报文格式 上图中有几个字段需要重点介绍下:      ...

  10. 三大域对象的使用总结request域 + session域 +

    request域: servlet 产生数据显示完后,就没用了. session域:数据除了显示之外,等一会还要用. servletContext域: 数据除了显示之外不仅还要用还要给其他人用.