【Leetcode】【Medium】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.
解题思路:
1、对原字符串进行排序,这样所有具有相同字符的字符串就得到了相同的结果。
2、使用hash表,key是排序后的字符串,value设置为结果数组中保存的地址(下标),这样,找打一个重复字符串就可以直接放入结果数组中。
注意:
1、返回结果中,对于相同字符的字符串,要按照字典序进行排序,因此还要多一步排序过程。
解题步骤:
1、新建结果数组和hash表;
2、遍历输入的原始字符串集合,对每个字符串:
(1)排序
(2)在hash中查找,如果找到,则取出value,对应到结果数组中,将原字符串插入;
(3)如果在hash表中没找到,则:
a. 先在结果二维数组中开辟新的一维数组,用来保存这个新字符组合;
b. 插入当前这个字符串;
c.在hash表中添加记录;value值是开辟的新数组位置;
3、最后遍历结果数组,对每个字数组进行排序;
AC代码:
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > ans;
unordered_map<string, int> mp;
for (int i = ; i < strs.size(); ++i) {
string cur(strs[i]);
sort(cur.begin(), cur.end());
if (mp.find(cur) != mp.end()) {
ans[mp[cur]].push_back(strs[i]);
} else {
vector<string> tmp(, strs[i]);
ans.push_back(tmp);
mp[cur] = ans.size() - ;
}
}
for (int i = ; i < ans.size(); ++i) {
sort(ans[i].begin(), ans[i].end());
}
return ans;
}
};
【Leetcode】【Medium】Group Anagrams的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode每天一题】Group Anagrams(变位词组)
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- WebStorage 和 Cookie的区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...
- mgo-后续测试(指定字段,获取id)
测试完mgo中的DBRef后,想接着测试指定字段的显示,才发现原来采用框架编码,很多问题被隐藏了起来: 1.显示指定字段: 之前在使用mgo时一直是查询全部字段,在mongo终端环境写为如下格式: & ...
- shopnc 二次开发 每日签到积分领取
/* 开始shopnc!!!!! url:xxx.com/index.php?act=index&op=userjf 一个四线城市的半吊子程序员~ 实现:前台模板文件 随便加入<a> ...
- kafka使用
0: ./sbt update ./sbt package ./sbt assembly-package-dependency 1: 启动ZK: 通过kafka的命令启动:bin/zookeeper- ...
- VC++ 中简单操作MP3音乐的方法,小结
#include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellap ...
- VC++ 结束线程 AfxBeginThread AfxEndThread
如果你的线程是从CWinThread继承出来的,结束自己就用AfxEndThread, 如果是外部调用的话,可以用PostThreadMessage(m_nThreadID, WM_QUIT,0,0) ...
- js中的引用类型-object
- 写一个ajax程序就是如此简单
写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...
- navicat for mysql
下载地址:https://www.navicat.com/cht/download 详情:http://baike.baidu.com/link?url=zo3CUg3HC5XUHkz4YqXO6Em ...
- HTML小知识---Label
今天知道了一个html小知识: <input type="checkbox" id="chkVersion" /> ...