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;
    }
};
时间: 2024-12-19 10:32:37

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 in lower-case. 解题思路:首先要理解,什么是anagrams,ie.“tea”.“tae”.“aet”,然后就十分好做了,new一个hashmap,使用一个排过序的String作为key,重复了就往里面添加元素,这里出现一个小插曲,就是排序的时候不要用PriorityQueue,因为P

049 Anagrams

049 Anagrams 这道题就是简单的哈希表了 from collections import defaultdict class Solution: # @param {string[]} strs # @return {string[]} def anagrams(self, strs): dic = defaultdict(list) ans = [] for s in strs: dic["".join(sorted(s))].append(s) for v in dic.

[LeetCode][Java] Anagrams

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意: 给定一个字符串数组,返回所有的易位构词组合. 注意:所有的输入都是小写. 算法分析: 易位构词其实也很好理解,就是两个单词所包含的字符和数量都是一样的,只是顺序不同 对字符串中各字母进行排序,那么互为重排列的字符串就会相等. 按照上述思路,用一个map纪

leetcode 49. Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. [Solution] FOR ANY string st IN strs, 1) sort st 2) hash st, since st is sorted, all anagrams will be hash to one value. 1 vector<strin

【leetcode】Anagrams (middle)

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. anagrams 的意思是两个词用相同的字母组成  比如 “dog" "god" 思路: 把单词排序 如 dog 按字母排序变为 dgo 用unordered_map<string, int> 记录排序后序列第一次出现时,字符串在输入st

【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:即字母个数和字母都相同,但是字母顺序不相同的词 e.g. "tea","and","ate","eat","dan".   return &qu

Leetcode 之Anagrams(35)

回文构词法,将字母顺序打乱.可将字母重新排序,若它们相等,则属于同一组anagrams. 可通过hashmap来做,将排序后的字母作为key.注意后面取hashmap值时的做法. vector<string> anagrams(vector<string> &strs) { unordered_map<string, vector<string>> group; for (const auto &s : strs) { string key

LeetCode – Refresh – Anagrams

Use a hash table to record it. The tricky part is that when the hash value is > 0, it is the index + 1. Otherwise, that string already exists in result list. 1 class Solution { 2 public: 3 vector<string> anagrams(vector<string> &strs) {

【LeetCode】Anagrams 解题报告

[题目] Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. [解析] 题意:给定一个字符串数组,返回所有是"换位词"的字符串. 所谓"换位词/变位词"就是包含相同字母,但字母顺序可能不同的字符串.比如"abc", "bca", "cab&q