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 ...
随机推荐
- 【spring boot】使用注解@ConfigurationProperties读取配置文件时候 报错 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependenc
如题,配置文件如下: #注册中心配置 eureka: instance: instanceId: ${spring.application.name}:${random.int} hostname: ...
- Android 花钱 划动手指每天一元钱
花钱(英文ColorMoney)是由上海花动传媒开发的一款免费的应用程序,现支持Android操作系统.安装花钱app后,用户将获得全新的手机锁屏背景图,其中包含各种有趣.唯美的壁纸类图片,亦包含应用 ...
- 二十四种设计模式:模板方法模式(Template Method Pattern)
模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...
- NGUI自适应屏幕分辨率
unity官方承诺的新ui系统一直没有推出来,我们的UI使用的是原生的OnGUI系统,刚好UI需要改版,索性就想迁到NGUI上面来,于是看了一下NGUI源码,发现NGUI可以大大的降低DrawCall ...
- [转]sa不能远程连接sql server 2008的解决办法
本文转自:http://www.cnblogs.com/chendaoyin/archive/2012/08/25/2656900.html 方法: 开始->Microsoft SQL Serv ...
- javascript上传多张图片并预览
直接上代码 html代码 <div> <label>封面</label> <input type="file" id="cove ...
- 解决在win系统下使用DOS命令开启TensorBoard的问题及方法步骤
解决在win系统下使用DOS命令开启TensorBoard的问题及方法步骤: TensorBoard是TensorFlow下的一个可视化的工具,能够帮助研究者们可视化训练大规模神经网络过程中出现的复杂 ...
- [NM 状态机2] Container状态机详解
概述 前面已经分析了RM的状态机,接下来将分析NM的状态机,NM状态机包括Container,Application,LocalizedResource三个,其中Container相对较复杂.现在我们 ...
- Microsoft.VisualStudio.DebuggerVisualizers.dll 文件位置 for VisualStudio 2015
可视化调试工具(Microsoft.VisualStudio.DebuggerVisualizers) "C:\Program Files (x86)\Microsoft Visual St ...
- Python描写叙述符(descriptor)解密
Python中包括了很多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...