【LeetCode】49. Anagrams (2 solutions)
Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
首先解释一下什么是anagrams:在不考虑顺序的情况下,包含相同字母的字符串组成anagrams
例如:
1、{"eat","ate","tea","coffee"}的anagrams是{"eat","ate","tea"}
2、{"tea","and","ate","eat","dan"}的anagrams是{"tea","ate","eat","and","dan"}
解法一:排序之后的string作为key
在具体实现过程中,构造了两张映射表dict和exist
dict用来对排序过的单词进行快速查找,值(value)为第一个该编码的单词下标。
exist用来记录该排序过的单词是否已存在anagrams中,
如果是,只需要将当前单词装入anagrams;
如果不是,首先要将存放在m中的代表该排序过的单词的第一个单词装入anagrams,再装入当前单词。
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ret;
unordered_map<string, int> dict;
unordered_map<string, bool> exist;
for(int i = ; i < strs.size(); i ++)
{
string temp = strs[i];
sort(temp.begin(), temp.end());
if(dict.find(temp) == dict.end())
dict[temp] = i;
else
{//find anagrams
if(exist[strs[dict[temp]]] == false)
{
ret.push_back(strs[dict[temp]]);
exist[strs[dict[temp]]] = true;
}
ret.push_back(strs[i]);
exist[strs[i]] = true;
}
}
return ret;
}
};

解法二:利用质因数分解
背景:任何一个正整数都可以分解为唯一的质因数乘积:n=2a3b5c7d…
因此n可以用{a,b,c,d}来唯一表示。
对于这个题,我们对a~z每个单词的出现次数作为每个质因数的次幂,
将乘积进行唯一编码,就可以忽略字母顺序了。相同编码的单词组成anagrams。
因为共有小写字母26个,因此我们需要前26个质因数。
在具体实现过程中,我还构造了两张映射表m和exist
dict用来对编码进行快速查找,值(value)为第一个该编码的单词下标。
exist用来记录该编码是否已存在anagrams中,
如果是,只需要将当前单词装入anagrams;
如果不是,首先要将存放在dict中的代表该编码的第一个单词装入anagrams,再装入当前单词。
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ret;
vector<long long int> code(strs.size(), );
CalCode(strs, code);
map<long long int, int> dict;
map<string, bool> exist;
for(int i = ; i < strs.size(); i ++)
{
if(dict.find(code[i]) == dict.end())
dict[code[i]] = i;
else
{
if(exist[strs[dict[code[i]]]] == false)
{
exist[strs[dict[code[i]]]] = true;
ret.push_back(strs[dict[code[i]]]);
}
exist[strs[i]] = true;
ret.push_back(strs[i]);
}
}
return ret;
}
void CalCode(vector<string> &strs, vector<long long int> &code)
{
for(int i = ; i < strs.size(); i ++)
{
string cur = strs[i];
vector<int> count(, ); //a~z count
for(int j = ; j < cur.size(); j ++)
{
count[cur[j]-'a'] ++;
}
double prime[] = {,,,,,,,,,,,,,,,,,,,,,,,,,};
long long result = ;
for(int j = ; j < ; j ++)
{
result *= (long long int)pow(prime[j], count[j]);
}
code[i] = result;
}
}
};

【LeetCode】49. Anagrams (2 solutions)的更多相关文章
- 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...
- 【LeetCode】49. Group Anagrams
题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...
- 【LeetCode】49. 字母异位词分组
49. 字母异位词分组 知识点:字符串:哈希表 题目描述 给你一个字符串数组,请你将 字母异位词 组合在一起.可以按任意顺序返回结果列表. 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源 ...
- 【LeetCode】18. 4Sum (2 solutions)
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 【LeetCode】46. Permutations (2 solutions)
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】120. Triangle (3 solutions)
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- 【LeetCode】78. Subsets (2 solutions)
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【LEETCODE】49、数组分类,简单级别,题目:566,1089
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- 安装在virtualbox中的kali linux如何配置无线网卡
1.安装在virtualbox里的kali无法使用所在物理机的wifi 2.必须通过usb方式,如图所示 3.所使用的usb无线网卡必须是kali2.0支持的 4.我的型号是TL-WN823N 2.0 ...
- C++中List的用法
Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() 给list赋值 back() 返回最后一个元素 begin() ...
- ubuntu14.04开启root用户 设置root密码 配置国内镜像源 设置分辨率
一.Ubuntu 默认是不允许 root 通过 ssh 直接登录的,可以修改 /etc/ssh/sshd_config,设置 1 PermitRootLogin yes 然后重启 ssh 服务即可 1 ...
- C#线程同步与死锁Monitor
在上一讲介绍了使用lock来实现C#线程同步.实际上,这个lock是C#的一个障眼法,在C#编译器编译lock语句时,将其编译成了调用Monitor类.先看看下面的C#源代码: public stat ...
- Android中Fragment的简单介绍
Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自 ...
- cocos2dx游戏存储举例及其注意事项
今天白白跟大家分享一下cocos2dx中游戏的存储及需要注意的事项 cocos2dx中自带了存储类:CCUserDefault ,倘若需要存储的数据量教大的话,建议使用数据库来存储 现在先给大家看一下 ...
- BNU Concentric Rings
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16030 Concentric Rings There are several different ...
- 独立开发人员低成本推广APP的18条技巧
导语:知道并不等于运行,有些最主要的推广方法往往会被忽略.这些,是自国外开发人员总结出的这18条经验. 如今市面上充满了大牌子大公司和大制作的手机游戏,常常有游戏花300万成本开发,然后再花2000万 ...
- OpenSSL 中 RSA 加密解密实现源代码分析
1.RSA 公钥和私钥的组成.以及加密和解密的公式: 2.模指数运算: 先做指数运算,再做模运算.如 5^3 mod 7 = 125 mod 7 = 6 3.RSA加密算法流程: 选择一对不同的.而且 ...
- Sql server management studio: cannot find one or more components
Install VS2010 SHELL 独立组件 https://www.microsoft.com/en-US/download/details.aspx?id=1366 运行安装程序,rep ...