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 ...
随机推荐
- 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面
这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...
- 【chrome】在做项目使用chrome调试的时候,调整Console的位置
在新的电脑上安装了谷歌浏览器 ,然后在调试系统的时候,发现console这个控制台,模拟调试js的位置无法显示到source以下, 解决问题: 怎么样让console控制台显示到sources下,在查 ...
- Android内存优化5 了解java GC 垃圾回收机制3
引言 接App优化之内存优化(序), 作为App优化系列中内存优化的一个小部分. 由于内存相关知识比较生涩, 内存优化中使用到的相关工具, 也有很多专有名词. 对Java内存管理, GC, Andro ...
- xUtils怎样通过注解对FindViewById进行封装
之前讲到了介绍了一下xUtils的基本用法,今天我们就来具体介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它採用了一种注解的方式进行声明,那么我们首先 ...
- 【原创】Android自定义适配器的使用方法
比如说我们已经得到了数据,想在一个listview或者在其他的控件中显示的,并且我们显示想要自己设计样式来显示的话就要用到自定义适配器了,下面让我们结合代码讲一下具体的使用方法: 代码会有注释的哦: ...
- SQL PL/SQL语法手册
SQL PL/SQL语法手册 目 录 第一部分 SQL语法部分 3 一. CREATE TABLE 语句 3 二. CREATE SEQUENCE语句 5 三. CREATE VIEW语句 6 ...
- Tomcat的server.xml配置讲解(一)
一.Tomcat虚拟目录的配置 1.服务器配置 默认端口号为8080,如果要想修改端口号,则可以在Tomcat目录中的conf/server.xml文件,找到如下代码,将端口号改为:80:保存serv ...
- docker中的link
一.查看容器的详细情形 docker inspect 容器id/名称 二.为什么要用link 容器每次重启时,ip都会变动,这不利于前端引用中连接数据库. 三.link docker run -p 8 ...
- 如何为iTunes Connect准备应用
原地址:http://blog.sina.com.cn/s/blog_947c4a9f0101dded.html 如果你已经成功注册了iOS开发者,那么现在就可以登陆iTunes Connect来管理 ...
- java Web笔记
Get:标记在地址栏中 放在请求行中显示get?Username=...Password=... Post:使用的是二进制的上传下载 请求行不显示信息 数据传输量大数据隐蔽对文本没有 pst不能再浏览 ...