171. Anagrams【medium】
Given an array of strings, return all groups of strings that are anagrams.
Notice
All inputs will be in lower-case
Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"].
Given ["ab", "ba", "cd", "dc", "e"], return ["ab", "ba", "cd", "dc"].
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
解法一:
class Solution {
public:
/**
* @param strs: A list of strings
* @return: A list of strings
*/
string getSortedString(string &str) {
static int count[];
for (int i = ; i < ; i++) {
count[i] = ;
}
for (int i = ; i < str.length(); i++) {
count[str[i] - 'a']++;
}
string sorted_str = "";
for (int i = ; i < ; i++) {
for (int j = ; j < count[i]; j++) {
sorted_str = sorted_str + (char)('a' + i);
}
}
return sorted_str;
}
vector<string> anagrams(vector<string> &strs) {
unordered_map<string, int> hash;
for (int i = ; i < strs.size(); i++) {
string str = getSortedString(strs[i]);
if (hash.find(str) == hash.end()) {
hash[str] = ;
} else {
hash[str] = hash[str] + ;
}
}
vector<string> result;
for (int i = ; i < strs.size(); i++) {
string str = getSortedString(strs[i]);
if (hash.find(str) == hash.end()) {
continue;
} else {
if (hash[str] > ) {
result.push_back(strs[i]);
}
}
}
return result;
}
};
手动实现了字符串的排序,有些复杂,参考@NineChapter 的代码
解法二:
class Solution {
public:
/**
* @param strs: A list of strings
* @return: A list of strings
*/
vector<string> anagrams(vector<string> &strs) {
int size = strs.size(), i = ;
if (size <= ) {
return vector<string>();
}
vector<string> result;
map<string, int> hash;
for (i = ; i < size; i++) {
string temp = strs[i];
sort(temp.begin(), temp.end());
hash[temp]++;
}
for (i = ; i < size; i++) {
string temp = strs[i];
sort(temp.begin(), temp.end());
if (hash[temp] > ) {
result.push_back(strs[i]);
}
}
return result;
}
};
直接用STL的sort函数搞
171. Anagrams【medium】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 61. Search for a Range【medium】
61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- Luci实现框架
转自:http://www.cnblogs.com/zmkeil/archive/2013/05/14/3078774.html 1.总述 上一篇总结了uhttpd的工作方式,openwrt中利用它作 ...
- 关于push动画中尺寸问题
由于是在sb中写的VC, 所以在跳转动画时, 就会有一些问题. 这是sb中的约束: 当在push动画时, 在中间界面添加imageView时, 如图: imageView的尺寸是如上图所示, 并不是屏 ...
- [转]create a basic sql server 2005 trigger to send email alerts
本文转自:http://blog.netnerds.net/2008/02/create-a-basic-sql-server-2005-trigger-to-send-e-mail-alerts/ ...
- WSDL-学习总结
1.什么是WSDL 是一种使用 XML 编写的文档.这种文档可描述某个 Web service.它可规定服务的位置,以及此服务提供的操作(或方法). 2.WSDL文档结构: <binding&g ...
- 关于json对象的删除
摘自:http://xosadan.iteye.com/blog/1100383 关于json对象的删除 一个json对象在后台产生了,但是有些数据可能无效或者不合法,所以需要在前台作些例外处理,比如 ...
- Java笔记15:多线程
Java实现多线程有两种方式:一是继承Thread类:二是实现Runable接口. 一.Thread实现 publicclass ThreadDemo2 { publicstaticvoid main ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- Python监控进程性能数据并画图保存为PDF文档
引言 利用psutil模块(https://pypi.python.org/pypi/psutil/),可以很方便的监控系统的CPU.内存.磁盘IO.网络带宽等性能參数,下面是否代码为监控某个特定程序 ...
- Unity3D Gamecenter 得分上传失败的处理
Unity3DGamecenter 得分上传失败的处理. 常常会有种情况是,玩家在地铁或者飞机上没法通过wifi或者3G连接到服务器,从而会导致上传分数失败 解决方法: 这时候需要把得分存在Playe ...
- c++中的对象复制
(1)this指针 this是一个隐含于每个类的成员函数的特殊指针,该指针是一个指向正在被某个成员函数操作的对象的指针. 当一个对象调用成员函数时,编译程序先将对象的地址赋给this指针,也就是说,当 ...