leetcode个人题解——#49 Group Anograms
思路:利用c++ stl的map来实现关键字匹配,
遍历strs容器类,对其中每一个string进行按字典序排序后,查找是否存在这样一个键,如不存在,存储该键,并将str[i]作为键映射的第一个元素;如存在,将str[i]添加到该键映射的vector<string>里。
最后用迭代器将map的映射即iter->second复制到题目要求返回的类型容器中。
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
typedef map<string, vector<string>> mstring;
mstring res;
const int Size = strs.size();
for(int i = ;i < Size; i++){
string key = strs[i];
sort(key.begin(), key.end());
if(res.find(key) == res.end()) res[key] = {strs[i]};
else
res[key].push_back(strs[i]);
}
vector<vector<string>> ans;
for(mstring::iterator iter = res.begin(); iter != res.end(); iter++)
ans.push_back(iter->second);
return ans;
}
};
leetcode个人题解——#49 Group Anograms的更多相关文章
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- 刷题49. Group Anagrams
一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...
- LeetCode 中等题解(4)
40 组合总和 II Question 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
随机推荐
- Objective-C 之深拷贝和浅拷贝
3月箴言 人的思想是了不起的,只要专注于某一项事业,就一定会做出使自己感到吃惊的成绩来.—— 马克·吐温 1.iOS中关于深拷贝和浅拷贝的概念 浅拷贝:浅拷贝并不拷贝对象本身,只是对指向对象的指针进行 ...
- webpack+vuecli使用问题总结
1,按照官网安装步骤install $ npm install -g vue-cli $ vue init webpack my-project $ cd my-project $ npm insta ...
- CentOS7集成Apache和SVN
本文主要介绍如何在CentOS7环境下集成Apache和SVN,完成后可以通过浏览器访问SVN仓库. 1.查看系统环境,关闭防火墙和SELinux. [root@Docker /]# systemct ...
- Centos7下python3.7的pipSSLError问题
在Centos7下成功安装了python3.7, 但是在使用pip3的时候出现了以下错误 中间试过了网上大家给出的好多种办法,一开始我只是在编译的时候 ./configure --with-ssl(p ...
- vue-scroller使用
<template> <div class="page page-scroller"> <scroller class="scroller& ...
- input的默认样式去除
outline:none;-----可去除input=text,的输入框输入时的亮边.
- linux-2.6.22.6内核启动分析之配置
配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- SQL 查询某时间段的数据 datadiff 计算时间差
datediff语法格式:datediff(day,开始时间,结束时间) 一.应用举例: 上面的代码,将查询 'created_time' > '2016-09-20' 的所有记录. 如果要查 ...
- Matlab_GUI
1.GUI中控件的属性 BackgroundColor 控件的背景 FontSize 控件字体的大小