题目

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. cxf 生成客户端代码调用服务

    cxf是另一种发布webservice的方式,与jdk提供的相比 jdk提供的是wsimport cxf 提供的是 wsdl2java- d 地址 根据http://www.cnblogs.com/f ...

  2. 【转载】正则过滤所有html标签,只留文字的方法。

    public static string Html2Text(string htmlStr) { if (String.IsNullOrEmpty(htmlStr)) { return "& ...

  3. Android Native IPC 方案支持情况

    Binder - 不支持Native层的binder 内存共享 - 不支持 信号量(信号灯) - 不支持 消息队列 - 不支持 信号 - 支持,但是不能用sigqueue传消息,只能用来安装信号,可以 ...

  4. HDU 4927 Series 1 ( 组合+高精度)

    pid=4927">Series 1 大意: 题意不好翻译,英文看懂也不是非常麻烦,就不翻译了. Problem Description Let A be an integral se ...

  5. ItelliJ项目打jar包

    不是Eclipse里方便的export...了. 一.配置 . 点击View->Open Module Settings(快捷键是F4) . 在弹出的对话框中,点击最左侧树的Artifacts ...

  6. jQuery常用方法一览及JQuery选择器获取表格中按钮所在行的其他列值

    Attribute: $(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”test Image”}); ...

  7. HttpPutFormContentFilter 和 ContextLoaderListener 讲解

    1 ContextLoaderListener 继承自ContextLoader,并且实现ServletContextListener接口. 肯定得实现这个接口了,不然怎么作为Servlet的监听器呢 ...

  8. unity, 忽略碰撞

    一,layer之间忽略碰撞. Edit->Project Settings->Physics->Layer Collision Matrix 二,collider之间忽略碰撞. vo ...

  9. 安装wxWidgets遭遇的两大关卡

    早就想体验wxWidgets.这学期的C++课,课时还算充裕.关键是弟子们的实践能跟得上,我希望能让他们也浅尝一把GUI开发. MFC能够选.但既然IDE都用CodeBlocks了.还是选wxWidg ...

  10. 算法图绘制工具Graphviz

    graphviz是贝尔实验室设计的一个开源的画图工具,它的强大主要体现在“所思即所得"(WYTIWYG,what you think is what you get),这是和office的“ ...