【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 ...
随机推荐
- 云主机不能外网ssh连接,只能内网ssh连接的问题处理
某台服务器外网无法ssh,内网可以ssh连接,ping值延时比较大 安装iftop查看流量 yum install -y iftop iftop界面含义如下 第一行:带宽显示 中间部分:外部连接列表, ...
- 15 个有用的 MySQL/MariaDB 性能调整和优化技巧(转载的一篇好文)
MySQL 是一个强大的开源关系数据库管理系统(简称 RDBMS).它发布于 1995 年(20年前).它采用结构化查询语言(SQL),这可能是数据库内容管理中最流行的选择.最新的 MySQL 版本是 ...
- Reactjs 入门基础(二)
如果我们需要向组件传递参数,可以使用 this.props 对象,实例如下: <body> <div id="example"></div> & ...
- 第九十八天请假 PHP TP框架 空操作和命名空间
访问空方法解决办法->在控制器里面创建 _empty 方法 <?php namespace Home\Controller; use Think\Controller; class Ind ...
- win7,win8.1下hosts文件无法修改的快速解决办法
一,找到C:\Windows\System32\drivers\etc,下hosts文件复制一份到桌面: 二,使用notepad++或其他编辑器修改桌面复制出来的那份HOSTS: 三,将修改后的文件复 ...
- perl中读取外部文件
打开一个在电脑G盘111文件下的一个文件 #!/usr/bin/perl -w use strict; open(IN,"G:/111/mylove.txt"); while($ ...
- hibernate学习(设计一对一 关系 映射)
//主表 package org.crazy.app.domain; import javax.persistence.*; @Entity @Table(name="person_inf& ...
- jQuery数组处理汇总
jQuery数组处理汇总 有段时间没写什么了, 打算把jquery中的比较常用的数组处理方法汇总一下 $.each(array, [callback])遍历,很常用 1 2 3 4 5 6 7 8 ...
- 5.HotSpot的算法实现
1.枚举根节点 在可达性分析中,可以作为GC Roots的节点有很多,但是现在很多应用仅仅方法区就有上百MB,如果逐个检查的话,效率就会变得不可接受. 而且,可达性分析必须在一个一致性的快照中进行-即 ...
- SDWebImage实现原理--两张图带你看懂
SDWebImage底层实现有沙盒缓存机制,主要由三块组成:1.内存图片缓存,2.内存操作缓存,3.磁盘沙盒缓存 SDWebImage GitHub地址 版本4.0.0 一.SDWebImage时序图 ...