LeetCode: Anagrams 题解

Given an array of strings, return all groups of strings that are
anagrams.

Note: All inputs will be in lower-case.

题解:
判断字符串是否为回文构词法生成的。找出所有由同一回文构词法生成的字符串对。

使用map用于散列。 将strs中的字符串strs[i],在串内进行字典排序,生成key,原始s[i]不变。
将该字符串s[i]映射到key所对应位置。map[key].push_back(str[i]).

依次对字符串数组进行散列。

最后遍历map, 按照字典序将key挂载字符串大于1的位置元素输出到结果vector中。


 1 class Solution {
2 public:
3 vector<string> anagrams(vector<string> &strs) {
4 int i;
5 map<string, vector<string> > vset;
6 for(i=0;i<strs.size();i++)
7 {
8 string key = strs[i];
9 sort(key.begin(),key.end());
10 vset[key].push_back(strs[i]);
11 }
12 vector<string> result;
13 map<string, vector<string> >::iterator iter;
14 for(iter = vset.begin();iter!=vset.end(); iter++)
15 {
16 if(iter->second.size()>1)
17 {
18 vector<string>::iterator viter;
19 for(viter=iter->second.begin(); viter!=iter->second.end();viter++)
20 result.push_back(*viter);
21 }
22 }
23 return result;
24 }
25 };

转载请注明出处:http://www.cnblogs.com/double-win/ 谢谢。

时间: 2024-10-26 23:55:56

LeetCode: Anagrams 题解的相关文章

LeetCode: palindromes 题解

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

[leetcode]Anagrams @ Python

原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:anagram的意思是:abc,bac,acb就是anagram.即同一段字符串的字母的不同排序.将这些都找出来.这里使用了哈希表,即Python中的dic

LeetCode: LetterCombinations 题解

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode: Anagrams [048]

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

LeetCode: plusOne 题解

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意: 给定一个由一组数字构成的数组表示的非负整数, 对这个数进行加一操作,并将结果数组返回. 数组的首元素存储的是该非负整数的最高位. 本题比较简

LeetCode ---Anagrams() 详解

Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. For example: Input: ["tea","and","ate","eat","den"] Output:   ["tea",&qu

[leetcode] Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路: 什么是anagrams?傻逼了,百度吧.一百度全是别人的方法,看到了有map存在,正是这个提示导致此题并没有用很长的时间. 方法很简单,将vector中的每个string先排序,然后在map中寻找是否已经有排序后的string,如果有则说明找到

LeetCode——Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 原题链接:https://oj.leetcode.com/problems/anagrams/ 易位构词游戏的英文词汇是 anagram,这个词来源于有"反向"或"再次"的含义的希腊语字根ana-和有"书写"."

LeetCode(100)题解--Same Tree

https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 思路:  DFS AC代码: 1.递归 1