Leetcode#49 Anagrams
Anagram:变位词。两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同
第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么。。后来还是看网上其他人的总结知道是怎么回事。
通常的做法是:把字符串内的字符排序,这样,凡是变位词都会变成相同的单词。用map记录这样的单词出现了几个,如果超过1个,则加入结果集中。
代码:
vector<string> anagrams(vector<string> &strs) {
vector<string> res;
map<string, int> record; for (int i = ; i < strs.size(); i++) {
string tmp = strs[i];
sort(tmp.begin(), tmp.end());
map<string, int>::iterator it = record.find(tmp);
if (it == record.end())
record.insert(pair<string, int>(tmp, ));
else
it->second++;
} for (int i = ; i < strs.size(); i++) {
string tmp = strs[i];
sort(tmp.begin(), tmp.end());
map<string, int>::iterator it = record.find(tmp);
if (it->second > )
res.push_back(strs[i]);
} return res;
}
Leetcode#49 Anagrams的更多相关文章
- [Leetcode][Python]49: Anagrams
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 49: Anagramshttps://leetcode.com/proble ...
- LeetCode 49: 字母异位词分组 Group Anagrams
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- LeetCode 49 Group Anagrams(字符串分组)
题目链接: https://leetcode.com/problems/anagrams/?tab=Description Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- 【LeetCode】49. Anagrams (2 solutions)
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- (LeetCode 49)Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- tp框架集成支付宝,中转页变成gbk编码
tp框架中集成支付宝的功能,将支付宝的demo例子存在到下图位置\Extend\Vendor\Alipay 生成支付订单 /** * 支付订单 */ public function pay() { h ...
- mysql 格式化时间
SELECT phone,chang, msg, linkid, DATE_FORMAT(mo_time, '%Y%m%d%H%i%s') FROM mo http://www.w3school.co ...
- centos yum 安装问题
yum [Errno 256] No more mirrors to try 解决方法 输入下面的命令即可解决问题: yum clean all yum makecache 导致 centos安装软件 ...
- delphi 基础之四 delphi 组织结构
delphi 组织结构 在Delphi中,一个正在开发的应用程序可以被称作项目或者工程.一般地,一个项目主要由dpr(项目).pas(单元)和dfm(窗体)三种文件组成,另外还有一些附属文件,如res ...
- DevExpress GridControl 部分用法
1.GridControl赋值:this.GridControl1.DataSouce=dt; 2.GridContro总合计及分组合计: 常规总合计直接RunDesigner-Group Summa ...
- 用C#写的读写CSV文件
用C#写的读取CSV文件的源代码 CSV文件的格子中包含逗号,引号,换行等,都能轻松读取,而且可以把数据转化成DATATABLE格式 using System; using System.Text; ...
- delphi中的ClientDataSet组件的open和Execute方法各自在什么情况下用?
ClientDataSet组件本来是给midas用的,也是所谓的borland的三层数据技术,使用这个控件必须发行midas.dll挺麻烦的 open是通过应用的SQL语句为SELECTexecute ...
- 【javascript】随手记代码
//js实现的当前界面的刷新.前进.后退 <input type="button" value="刷新" onclick="window.loc ...
- 网络开发库从libuv说到epoll
引言 这篇博文可能有点水,主要将自己libuv的学习过程和理解. 简单谈方法. 有点杂. 那我们开始吧. 首先介绍 githup . 这个工具特别好用. 代码托管. 如果不FQ可能有点卡. 但是应该试 ...
- LightOJ 1317 第八次比赛 A 题
Description You probably have played the game "Throwing Balls into the Basket". It is a si ...