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 ...
随机推荐
- 【微信小程序】request请求POST提交数据,记得要加上header
wx.request({ url: '*******', data: { "type":"nearest_village", "district&qu ...
- 金软PDF转换(x-PDFConper)
一.产品概述 金软PDF转换(x-PDFConper)软件产品是一款能够在手机.平板电脑等移动终端设备上.能够浏览MS Office 的Word.Excel.PowerPoint.PDF 等四种格式文 ...
- C# BackgroundWorker的Bug???
废话不多说,上代码: public partial class Form1 : Form { BackgroundWorker _bgWorker; int count; public Form1() ...
- php getallheaders使用注意事项
This function is an alias for apache_request_headers(). Please read the apache_request_headers() doc ...
- python pandas groupby
转自 : https://blog.csdn.net/Leonis_v/article/details/51832916 pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对 ...
- mysql numberic types ---- mysql 数值类型详解
编程语言中大多都有数据类型一说.虽然mysql 的sql 语句与标准sql 有别.但是宏观上看还是差不多的:下面我们说一下mysql数据库中的数值类型 一.在mysql里有那些类型可以表示数值: 1. ...
- vsftp 无法启动,500 OOPS: bad bool value in config file for: anonymous_enable
朋友的FTP启动不了,叫我帮他看,启动时出现以下错误信息: 500 OOPS: bad bool value in config file for: anonymous_enable 看似配置文件错误 ...
- path方法总结
$.mobile.path.get(url);//获取URL地址的目录部分,就是除了a.html之外的那部分 jQuery.mobile.path.getDocumentBase(bool) //获取 ...
- 03、同事分享课程的笔记 —《Android应用低功耗设计》
这是安卓组的同事一个月前分享的一节课程,听课时写了一下笔记,之前是写在本子上的,感觉内容挺不错 的,就保存在博客了吧,方便回看. 他曾经在就职于英特尔公司,是与芯片设计相关的,这课程标题虽然是与安卓相 ...
- PHP面对对象总结
一个关于面对对象知识的问答总计:https://wenku.baidu.com/view/391eeec483c4bb4cf6ecd1ad.html 面对对象的三大特征: 1.封装 为了保护类封装了之 ...