Leetcode: Anagrams(颠倒字母而成的字)
题目
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
思路
1. 使用数组模拟哈希表, 数组下标0-25分别代表字符'a'-'z', a[0] 代表 'a' 在单词中出现的次数
2. 排序, 只有相邻的单词才有可能是相同的
3. 这么慢的方法没想到 176ms 就能通过
总结
1. word 起初没有对 char 数组初始化, 结果 VS 成功运行, 但 Leetcode 上却是 WA. VS 真是宠坏一批程序员
代码
class word {
public:
word() {
memset(chars, 0, sizeof(chars));
index = 0;
}
int chars[26];
int index;
bool operator<(const word &ths) const {
for(int i = 0; i < 26; i ++) {
if(this->chars[i] != ths.chars[i]){
return this->chars[i] < ths.chars[i];
}
}
return this->index < ths.index;
}
bool operator==(const word &ths) const {
for(int i = 0; i < 26; i ++) {
if(this->chars[i] != ths.chars[i]) {
return false;
}
}
return true;
}
};
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<word> record;
for(int i = 0; i < strs.size(); i ++) {
record.push_back(buildWord(strs[i], i));
}
sort(record.begin(), record.end());
vector<word> res;
bool flag = false;
for(int i = 1; i < record.size(); i ++) {
if(record[i] == record[i-1]) {
if(!flag) {
res.push_back(record[i-1]);
res.push_back(record[i]);
flag = true;
}else{
res.push_back(record[i]);
}
}else{
flag = false;
}
}
return decomposition(res, strs);
}
word buildWord(const string &str, const int &index) {
word newword;
for(int i = 0; i < str.size(); i ++) {
newword.chars[str[i]-'a'] ++;
}
newword.index = index;
return newword;
}
vector<string> decomposition(vector<word> &vec, vector<string> &strs) {
vector<string> res;
for(int i = 0; i < vec.size(); i++) {
res.push_back(strs[vec[i].index]);
}
return res;
}
};
Leetcode: Anagrams(颠倒字母而成的字)的更多相关文章
- [Leetcode] Anagrams 颠倒字母构成词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)
最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...
- C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)
最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...
- AC日记——将字符串中的小写字母换成大写字母 openjudge 1.7 13
13:将字符串中的小写字母转换成大写字母 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个字符串,将其中所有的小写字母转换成大写字母. 输入 输入一行,包含一个字符串(长度不 ...
- HDOJ/HDU 1161 Eddy's mistakes(大写字母转换成小写字母)
Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for e ...
- Javascript 将一个句子中的单词首字母转成大写
Javascript 将一个句子中的单词首字母转成大写 先上代码 function titleCase(str) { str = str.toLowerCase().split(" &quo ...
- LeetCode Anagrams My solution
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- C#字母转换成数字/数字转换成字母 - ASCII码转换
字母转换成数字 byte[] array = new byte[1]; //定义一组数组arrayarray = System.Text.Encoding.ASCII.GetBytes(strin ...
随机推荐
- 【linux】FTP添加用户,设置权限和目录
一.目的,新建一个用户 test2,登录ftp,它只有自己的主目录权限,其他同级和上级目录没有权限 二.ftp安装.配置 yum -y install vsftpd //通过yum来安装vsftpd ...
- Python-线程的生命周期
线程的生命周期 所谓的xx生命周期,其实就是某对象的包含产生和销毁的一张状态图.线程的生命周期如下图所示: 各状态的说明如下: New新建.新创建的线程经过初始化后,进入Runnable状态. Run ...
- Using AutoFac
第一次整理了下关于autofac的一些具体的用法 1. 安装 Autofac: Install-Package Autofac -Version 4.8.1 2. 创建两个类库项目,IService ...
- 摘:C/C++中时间类time.h
摘要:本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时.时间的获取.时间的计算和显示格式等方面进行了阐述.本文还通过大量的实例向你展示了time.h头文件中 ...
- 【C语言】二维数组中的查找,杨氏矩阵
//二维数组中的查找,杨氏矩阵 //在一个二维数组中,每行都依照从左到右的递增的顺序排序.每列都依照从上到下递增的顺序排序. //请完毕一个函数.输入这种一个数组和一个数,推断数组中是否包括这个数. ...
- 计算机的OSI和TCP/IP网络模型
1.计算机的网络模型分为两种OSI模型和TCP/IP模型,它们的对应关系如下: 2.针对OSI模型,每一层都有各自的功能. 应用层 应用层是OSI模型中最靠近用户的一层,负责为用户的应用程序提供网 ...
- 细说websocket - php篇(未完)
下面我画了一个图演示 client 和 server 之间建立 websocket 连接时握手部分,这个部分在 node 中可以十分轻松的完成,因为 node 提供的 net 模块已经对 socket ...
- Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript.
Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript. 1. 现象::主程序卡住无反应,多行任务不往下执行 1 2. 原因::使用j ...
- 二叉排序树及其C代码
1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树, 或者是满足例如以下性质的二叉树 ...
- CXF学习笔记 之 “注解”
@WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值为 Java 类的简单名称 + Service.(字符 ...