LeetCode(49)Group Anagrams
题目
Given an array of strings, group anagrams together.
For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],
Return:
[
[“ate”, “eat”,”tea”],
[“nat”,”tan”],
[“bat”]
]
Note:
For the return value, each inner list’s elements must follow the lexicographic order.
All inputs will be in lower-case.
分析
该题目要求是将给定的一组字符串数组,按照同构词(相同字母组成的单词)分类,每组单词按照字典排序。
这道题AC算法考察的主要是哈希的思想,这样才能保证时间在要求的范围内。
开始按照常规思路,几层循环,寻找同构词,保存。。。但是结果会出现Time Limited Exceed,下面将给出两种算法实现,供对比思考。
Time Limited Exceed 代码
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
if (strs.empty())
return vector<vector<string> >();
int len = strs.size();
vector<vector<string> > ret;
for (int i = 0; i < len; i++)
{
vector<string > sv;
string tmp1 = strs[i];
sv.push_back(tmp1);
sort(tmp1.begin(), tmp1.end());
for (int j = i + 1; j < len; j++)
{
string tmp2 = strs[j];
sort(tmp2.begin(), tmp2.end());
if (tmp1 == tmp2)
{
sv.push_back(strs[j]);
//将处理后的元素赋值为空
strs[j] = "";
}
}//for
//按字典排序该序列
sort(sv.begin(), sv.end());
//添加到结果vector
ret.push_back(sv);
}//for
return ret;
}
};
AC代码
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
if (strs.empty())
return vector<vector<string> >();
int len = strs.size();
//将字符串数组按照字典顺序排序
sort(strs.begin(), strs.end());
//存储结果
vector<vector<string> > ret;
//利用哈希思想构建map,将排序后相等的字符串存在相应的vector
map<string, vector<string>> mv;
for (int i = 0; i < len; i++)
{
string str = strs[i];
sort(str.begin(), str.end());
mv[str].push_back(strs[i]);
}
for (map<string, vector<string> >::iterator iter = mv.begin(); iter != mv.end(); iter++)
ret.push_back(iter->second);
return ret;
}
};
LeetCode(49)Group Anagrams的更多相关文章
- LeetCode(49): 字母异位词分组
Medium! 题目描述: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", ...
- LeetCode(49)-Valid Parentheses
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- Thinkphp入门 五 —模型 (49)
原文:Thinkphp入门 五 -模型 (49) [数据库操作model模型] model 模型 数据库操作 tp框架主要设计模式:MVC C:controller 控制器 shop/Li ...
- Qt 学习之路 2(49):自定义只读模型
Qt 学习之路 2(49):自定义只读模型 豆子 2013年5月5日 Qt 学习之路 2 18条评论 model/view 模型将数据与视图分割开来,也就是说,我们可以为不同的视图,QListView ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- C# 大文件上传
IHttpModule 分块上传大文件 IHttpModule 分块上传大文件 来源:http://www.cnblogs.com/HeroBeast/archive/2008/03/18/10848 ...
- HTML中a标签自动识别电话、邮箱
HTML中a标签自动识别电话.邮箱 联系电话:<a href="tel:010-88888888">010-88888888</a><br> 联 ...
- iOS 上传APP到AppStore 卡在 Authenticating with the iTunes store 提示
上传APP的时候,遇到了问题,一直卡在Authenticating with the iTunes store提示这里, 解决办法:在Application Loader里面登录需要上传APP的开发者 ...
- LightOj 1197 Help Hanzo (区间素数筛选)
题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围 ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- vue项目导出电子表格
方法一: 一.安装依赖(前面基本一样) npm install file-saver --save npm install xlsx --save npm install script-loader ...
- jsp的简介
https://www.w3cschool.cn/jsp/jsp-intro.html
- java IO流 复制图片
(一)使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // St ...
- win7+idea+maven搭建spark源码阅读环境
1.参考. 利用IDEA工具编译Spark源码(1.60~2.20) https://blog.csdn.net/He11o_Liu/article/details/78739699 Maven编译打 ...
- JS获取服务器端控件ID
很多时候我们需要在JS中对服务器端控件进行一些简单处理,但是这个时候没有必要回发到服务器,让服务器去处理,这个时候就又要用到JS了 那么怎么去获取这个服务器端控件呢?我们知道服务器最终返回到用户界面的 ...