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. python 中类方法@classmethod

    classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @classmethod def f(cls, arg1, arg2, .. ...

  2. NFS配置项no_root_squash和root_squash的区别

    1.鸟哥的私房菜简体中文 http://linux-vbird.hillwood.cn/linux_server/0330nfs.htm 鸟哥的私房菜繁体中文 http://linux.vbird.o ...

  3. 设置三思LED的IP地址跟端口号

    出厂设置是:202.11.11.01 初始端口号是:2929 设置虚拟机的ip跟LED的ip在一个网段上,在虚拟机上telnet命令,登陆到LED上面. 在/etc/init.d/rcS文件中, #! ...

  4. CUBRID学习笔记 11 数据类型之日期

    datetime 虽然和mysql很相像,但是日期类型和mysql是不一样的.和sqlserver差不多. 如YYYY-MM-DD hh:mi:ss.fff or mm/dd/yyyy hh:mi:s ...

  5. So easy Webservice 1.Socket建设web服务

    socket 是用来进行网络通讯的,简单来说,远程机器和本地机器各建一个socket,然后通过该socket进行连接通讯 socket简单模型图: socket的原理图: 代码实现: 1.创建sock ...

  6. run a Freight robot (2)

    3.  Network Setup Connecting Freight to a Monitor The easiest way to configure the wireless networki ...

  7. mfc 可编辑 list control

    维护到一个古老的gm工具的时候 需要这个功能 在网上找到一份很好用的代码 贴到这里 再次感谢那位同僚 #pragma once //#include "OrangeMessage.h&quo ...

  8. 转 C编译: 使用gdb调试

    C编译: 使用gdb调试   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! gdb是the GNU Debugger的简称.它是 ...

  9. 删除List中制定的值的方法

    /** * * @param args */ public static void main(String[] args) { List<String> list = new ArrayL ...

  10. iOS - TouchLock 手势解锁

    1.手势解锁的创建 代码封装见 QExtension QLockView.h #import <UIKit/UIKit.h> @interface QLockView : UIView / ...