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中echo的使用方法
1.echo命令我们经常使用的选项有两个,一个是-n,表示输出之后不换行.另外一个是-e,表示对于转义字符按对应的方式处理,假设不加-e那么对于转义字符会按普通字符处理. 2.echo输出时的转义字符 ...
- kernel4.1 ioctl调用
在4.1内核中开发时遇到个奇怪的问题: 用户空间的ioctl无法调用到内核空间的unlock_ioctl 排查源码发现 546 int do_vfs_ioctl(struct file *filp, ...
- BZOJ 1603 [Usaco2008 Oct]打谷机 dfs
题意:id=1603">链接 方法:暴力 解析: 搜1到n路径上的边权异或和-. 这几个水题刷的我有点-.. 代码: #include <cstdio> #include ...
- spring in action 7.1 小结
0 AbstractAnnotationConfigDispatcherServletInitializer剖析,在Servlet 3.0环境中,容器会在类路径中查找实现ServletContaine ...
- 会话过程保存数据对象cookie和session
1 cookie是以键值对保存在浏览器端,服务器端可以创建.接收.发送 cookie 信息. request可以接收 cookie, response 可以发送 cookie. 1)cookie 可以 ...
- Nginx an upstream response is buffered to a temporary file,nginx502错误
1.错误日志:warn:an upstream response is buffered to a temporary file 解决办法:增加fastcgi_buffers 8 4K; fa ...
- widnows 使用WIN32 APi 实现修改另一打开程序的窗口显示方式
1.GUI点击打开一个程序那边做一个判断. hwnd = 获取目标程序窗口句柄: if(hwnd == NULL /*不存在目标程序窗口句柄*/){ 创建进程,打开目标程序: } else{ ...
- 解决Error: That port is already in use.
ubuntu系统下,运行一个django项目,即输入python manage.py runserver后,可能出现 Error: That port is already in use.的错误. 即 ...
- cocos2d-x中CCEditbox导出到lua
自从工作后感觉时间较少(每天工作9-22,晚上就不想动了,早上想多睡点),工作中用的是 cocos2d-x.cocos2d-x是一款手机游戏引擎,虽然支持lua,但和love2d相比非纯lua游戏引 ...
- 利用table-cell实现元素居中对齐
vertical-align对一些特定显示样式(例如单元格显示方式:table-cell)的元素才会起作用.所以要实现上下垂直居中对齐,可以采用如下样式 display:table-cell; ...