LeetCode 049 Anagrams
题目要求:Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
分析:
参考网址:http://www.cnblogs.com/easonliu/p/3643595.html
代码如下:
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
string s;
map<string, int> anagram;
vector<string> res;
for (int i = 0; i < strs.size(); ++i) {
s = strs[i];
sort(s.begin(), s.end());
if (anagram.find(s) == anagram.end()) {
anagram[s] = i;
} else {
if (anagram[s] >= 0) {
res.push_back(strs[anagram[s]]);
anagram[s] = -1;
}
res.push_back(strs[i]);
}
}
return res;
}
};
LeetCode 049 Anagrams的更多相关文章
- Java for LeetCode 049 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- [LeetCode] Group Anagrams 群组错位词
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- 【leetcode】Anagrams
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- 【leetcode】Anagrams (middle)
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Leetcode#49 Anagrams
原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...
- LeetCode 48 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- [LeetCode 题解]: Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Leetcode 之Anagrams(35)
回文构词法,将字母顺序打乱.可将字母重新排序,若它们相等,则属于同一组anagrams. 可通过hashmap来做,将排序后的字母作为key.注意后面取hashmap值时的做法. vector< ...
- careercup-排序和查找 11.2
11.2 编写一个方法,对字符串数组进行排序,将所有变位词1排在相邻的位置. 类似leetcode:Anagrams 解法: 变位词:由变换某个词或短语的字母顺序构成的新的词或短语.例如,“trian ...
随机推荐
- gdb高级技巧
注意: 这里是讲gdb的高级技巧.如果没有接触过gdb,请看这篇:点这里. gdb是一个功能极其强大的命令行调试器.其实,除了我们常用的 file b s n q disp p 等命令,也有很多高级技 ...
- c#方法 最大值我最小值
static void Main(string[] args) { int[] a = { 6, 8, 9, 5, 2, 165, 58966 }; Console.WriteLine("最 ...
- 理解js参数
<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title> ...
- canvas基础[二]教你编写贝塞尔曲线工具
贝塞尔曲线 bezierCurveTo 在线工具 https://canvature.appspot.com/ [感觉这个好用一些] https://blogs.sitepointstatic.com ...
- Java8 新特性 —— 函数式编程
本文部分摘录自 On Java 8 概述 通常,传递给方法的数据不同,结果也不同.同样的,如果我们希望方法被调用时的行为不同,该怎么做呢?结论是:只要能将代码传递给方法,那么就可以控制方法的行为. 说 ...
- Activit的心路历程:获取当前节点的下一节点【可能存在多个】的nodeId
上一任务节点 在我的开发任务中,突然给我提出了一个待办任务需要获取当前任务节点下一任务节点的表单信息,刚开始搞得我有点措手不及,后来仔细是靠后,灵感一下,直接操作流程的bpmn信息就可以获取到节点信息 ...
- gcc 执行c++报错
D:\cpp>g++ hello.cc Info: resolving std::cout by linking to __imp___ZSt4cout (auto-import) c:/min ...
- 第05组 Alpha冲刺 (1/6)
.th1 { font-family: 黑体; font-size: 25px; color: rgba(0, 0, 255, 1) } #ka { margin-top: 50px } .aaa11 ...
- Js中函数声明和函数表达式的区别
先看以下几段烧脑的代码: f();//=>? var f = function () { console.log("var"); } function f() { conso ...
- linux 进程间通信 共享内存 mmap
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进程A可以即时看到进程B对共享内存中数据的更新,反 ...