[CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other.
这道题让我们给一个字符串数组排序,让所有的变位词Anagrams排在一起,关于变位词,LeetCode里有两道相关的题目Anagrams 错位词和Valid Anagram 验证变位词。那么对于这道题,我们有两种方法可以实现,先来看第一种方法,来重写sort中的比较函数compare,参见代码如下:
解法一:
bool cmp(const string &a, const string &b) {
    string m = a, n = b;
    sort(m.begin(), m.end());
    sort(n.begin(), n.end());
    return !m.compare(n);
}
sort(array.begin(), array.end(), cmp);
另一种解法较为复杂一些,用到了哈希表来建立排序后的字符串和其所有的异位词集合的映射,最后在按集合填充原数组,参见代码如下:
解法二:
class Solution {
public:
    void sortArray(vector<string> &array) {
        unordered_map<string, vector<string> > m;
        for (auto &a : array) {
            string key = a;
            sort(key.begin(), key.end());
            if (m.find(key) == m.end()) {
                m[key] = vector<string>();
            }
            vector<string> &v = m[key];
            v.push_back(a);
        }
        int idx = ;
        for (unordered_map<string, vector<string> >::iterator it = m.begin(); it != m.end(); ++it) {
            for (auto &a : it->second) {
                array[idx++] = a;
            }
        }
    }
};
[CareerCup] 11.2 Sort Anagrams Array 异位词数组排序的更多相关文章
- Leetcode49. Group Anagrams字母异位词分组
		
给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan&quo ...
 - [CareerCup] 11.4 Sort the File 文件排序
		
11.4 Imagine you have a 20 GB file with one string per line. Explain how you would sort the file. 这道 ...
 - LeetCode 49: 字母异位词分组	Group Anagrams
		
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
 - [Swift]LeetCode49. 字母异位词分组 | Group Anagrams
		
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
 - [Swift]LeetCode438. 找到字符串中所有字母异位词 | 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 49. 字母异位词分组(Group Anagrams)
		
题目描述 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "ta ...
 - *438. Find All Anagrams in a String  找到字符串中所有字母异位词
		
1. 原始题目 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 201 ...
 - Leetcode438.Find All Anagrams in a String找到字符串中所有字母异位词
		
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: ...
 - C#版 - Leetcode49 - 字母异位词分组 - 题解
		
C#版 - Leetcode49 - 字母异位词分组 - 题解 Leetcode49.Group Anagrams 在线提交: https://leetcode.com/problems/group- ...
 
随机推荐
- Memcache笔记04-Memcached机制深入了解
			
Memcached机制深入了解 ①基于c/s架构 ,协议简单 c/s架构,此时memcached为服务器端,我们可以使用如PHP,c/c++等程序连接memcached服务器. memcached的服 ...
 - Entity Framework做IN查询
			
开发中遇到的Too high level of nesting for select错误 项目使用了Entity Framework结合Mysql, 遇到了一个非常奇怪的性能问题,一个看起来非常简单的 ...
 - Effective Java 47 Know and use the libraries
			
Advantages of use the libraries By using a standard library, you take advantage of the knowledge of ...
 - JRE与JDK的区别
			
转自:http://swiftlet.net/archives/639 1. 定义JRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者. ...
 - 虚拟机LVM扩容
			
1.先在Vmware上,把虚拟机硬盘做扩展 2.现在打开虚拟机发现系统的磁盘空间已经扩了,但是硬盘分区可用空间没变,还是原来的30G [root@localhost ~]# fdisk -l Disk ...
 - 面向对象and类
			
类和对象: 1.什么叫类:类是一种数据结构,就好比一个模型,该模型用来表述一类事物(事物即数据和动作的结合体),用它来生产真实的物体(实例). 2.什么叫对象:睁开眼,你看到的一切的事物都是一个个的对 ...
 - nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
			
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
 - esxi安装全过程及基本配置
			
esxi6.0下载地址 链接: http://pan.baidu.com/s/1jIfg2yU 密码: qacv 支持检测可以参考:http://www.linuxidc.com/Linux/2012 ...
 - MAC、IDFA、IMEI正则表达式
			
一.安卓: MAC:接入网络的设备的序号,唯一值.用 16 进制数表示,由 0-9,A-F 组成,如:44:2A:60:71:CC:82 Uuid 正则表达式: ^([0-9a-fA-F]{2})(( ...
 - 内存流和null字节
			
#include <stdio.h> #include <string.h> int main() { ]={}; FILE* fp = fmemopen(buf,," ...