题目

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(颠倒字母而成的字)的更多相关文章

  1. [Leetcode] Anagrams 颠倒字母构成词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  2. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  3. 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)

    最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...

  4. C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)

    最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...

  5. AC日记——将字符串中的小写字母换成大写字母 openjudge 1.7 13

    13:将字符串中的小写字母转换成大写字母 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个字符串,将其中所有的小写字母转换成大写字母. 输入 输入一行,包含一个字符串(长度不 ...

  6. HDOJ/HDU 1161 Eddy's mistakes(大写字母转换成小写字母)

    Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for e ...

  7. Javascript 将一个句子中的单词首字母转成大写

    Javascript 将一个句子中的单词首字母转成大写 先上代码 function titleCase(str) { str = str.toLowerCase().split(" &quo ...

  8. LeetCode Anagrams My solution

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  9. C#字母转换成数字/数字转换成字母 - ASCII码转换

    字母转换成数字 byte[] array = new byte[1];   //定义一组数组arrayarray = System.Text.Encoding.ASCII.GetBytes(strin ...

随机推荐

  1. (一)struts2入门——登陆验证熟悉struts2部署

    0.了解 Struts(支柱.支干)是什么? Struts是流行和成熟的基于MVC设计模式的Web应用程序框架.使用它能帮助我们减少在运用MVC设计模型来开发Web应用时间. 为什么2.1.3之后用S ...

  2. GNOME下也是Alt+F2,输入gnome-terminal

    如果桌面有terminal 的话 ,直接用上下键就可以了   Alt + F1 类似Windows下的Win键,在GNOME中打开”应用程序”菜单(Applications) Alt + F2 类似W ...

  3. 树莓派/RaspberryPi 内核源码下载

    树莓派的源码有两种下载方式:压缩包下载和git clone指令下载. 1.压缩包下载 选择对应分支,点击Github界面的 下载按钮即可,如下图: 测试发现,同样的分支,用压缩包方式下载后编译会出错, ...

  4. express@4.0.*

    $ sudo npm install -g express the installation goes fine. But when I try to create a project with ex ...

  5. tp数据库操作

    1.常见的数据库操作//插入记录// $insert=Db::execute("insert into tp_user (username,password) values ('dome', ...

  6. Mysql 数据库字符类型详解

    MySQL 中提供了多种对字符数据的存储类型,不同的版本可能有所差异.以5.0 版本为例,MySQL 包括了CHAR.VARCHAR.BINARY.VARBINARY.BLOB.TEXT.ENUM 和 ...

  7. AngularJS 中的作用域

    问题引入 使用 Angular 进行过一段时间的开发后,基本上都会遇到一个这样的坑: 123456789101112 <div ng-controller="TestCtrl" ...

  8. vim 创建文件自动生成头部注释

    知识点: 1. autocmd命令: 当读写一个文件时,自动执行指定的命令; 2. autocmd event: BufNewFile 当文件不存在时开始写文件; 3. exec命令 execute命 ...

  9. [转]软件测试 Top 120 Blog (博客)

    [转]软件测试 Top 120 Blog (博客) 2015-06-08 转自:    软件测试 Top 120 Blog (博客) # Site Author Memo DevelopSense M ...

  10. MySQL 使用 SSL 连接(附 Docker 例子)

    查看是否支持 SSL 首先在 MySQL 上执行如下命令, 查询是否 MySQL 支持 SSL: mysql> SHOW VARIABLES LIKE 'have_ssl'; +-------- ...