• 19.2.7 [LeetCode 49] Group Anagrams


    Given an array of strings, group anagrams together.

    Example:

    Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
    Output:
    [
      ["ate","eat","tea"],
      ["nat","tan"],
      ["bat"]
    ]

    题意

    把由相同字母出现相同次数组成的字符串归到一起(数据可能重复)

    题解

    一开始我当成了任何一种字母只会出现一次做了,用的是long型作二进制用,自然WA了,其实后来可以用字符串做,当时没想到,看了题解有人将字符串排序作为key,觉得挺有道理的

    class Solution {
    public:
        struct node {
            string str, after;
            int idx;
            node(string a,string b,int id):str(a),after(b),idx(id){}
            bool operator <(const node&b)const {
                if (after == b.after)
                    return idx < b.idx;
                return after < b.after;
            }
        };
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            set<node>all;
            for (int i = 0; i < strs.size(); i++) {
                string tmp = strs[i];
                sort(strs[i].begin(), strs[i].end());
                all.insert(node(tmp, strs[i], i));
                strs[i] = tmp;
            }
            auto pre = all.begin();
            vector<vector<string>>ans;
            vector<string>tmp;
            for (auto p = all.begin(); p != all.end(); p++) {
                if (p != all.begin() && (*p).after != (*pre).after) {
                    ans.push_back(tmp);
                    tmp.clear();
                }
                tmp.push_back((*p).str);
                pre = p;
            }
            ans.push_back(tmp);
            return ans;
        }
    };
    View Code

    顺便放一下用字符串记录字母出现次数做的,比上面那种慢,因为sort还是挺快的

     1 class Solution {
     2 public:
     3     struct node {
     4         string str, after;
     5         int idx;
     6         node(string a,string b,int id):str(a),after(b),idx(id){}
     7         bool operator <(const node&b)const {
     8             if (after == b.after)
     9                 return idx < b.idx;
    10             return after < b.after;
    11         }
    12     };
    13     vector<vector<string>> groupAnagrams(vector<string>& strs) {
    14         set<node>all;
    15         for (int i = 0; i < strs.size(); i++) {
    16             string tmp = "00000000000000000000000000";
    17             int l = strs[i].length();
    18             for (int j = 0; j < l; j++)
    19                 tmp[strs[i][j] - 'a']++;
    20             all.insert(node(strs[i], tmp, i));
    21         }
    22         auto pre = all.begin();
    23         vector<vector<string>>ans;
    24         vector<string>tmp;
    25         for (auto p = all.begin(); p != all.end(); p++) {
    26             if (p != all.begin() && (*p).after != (*pre).after) {
    27                 ans.push_back(tmp);
    28                 tmp.clear();
    29             }
    30             tmp.push_back((*p).str);
    31             pre = p;
    32         }
    33         ans.push_back(tmp);
    34         return ans;
    35     }
    36 };
    View Code
  • 相关阅读:
    将 SharePoint 2010 网站集升级到 2013 (含沙盒方案)
    几款网络云存储服务的使用对比
    技术发展飞快,一日十年
    项目背景介绍
    初次接触,简单的了解需求
    用色彩区分 SharePoint 2010 Calendar 的日历项
    嘿,我这里有一个 Survey!
    博客页面在 IE 浏览器中样式混乱了(已经更换了样式)
    关于 Graphviz
    搭建使用 RTX51Tiny 的 C51 Keil 项目环境
  • 原文地址:https://www.cnblogs.com/yalphait/p/10355329.html
Copyright © 2020-2023  润新知