leetcode_811_子域名访问计数

本文探讨如何处理LeetCode题目811,即根据输入的域名访问记录,计算每个子域名的访问次数。内容包括理解域名层级、解析输入格式及实现C++解决方案的关键步骤。
摘要由CSDN通过智能技术生成

一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 "com"。

给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。

接下来会给出一组访问次数和域名组合的列表cpdomains 。要求解析出所有域名的访问次数,输出格式和输入格式相同,不限定先后顺序。

示例 1:
输入: 
["9001 discuss.leetcode.com"]
输出: 
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
说明: 
例子中仅包含一个网站域名:"discuss.leetcode.com"。按照前文假设,子域名"leetcode.com"和"com"都会被访问,所以它们都被访问了9001次。
示例 2
输入: 
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
输出: 
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
说明: 
按照假设,会访问"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。
而对于父域名,会访问"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。

注意事项:

  •  cpdomains 的长度小于 100
  • 每个域名的长度小于100
  • 每个域名地址包含一个或两个"."符号。
  • 输入中任意一个域名的访问次数都小于10000

class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        unordered_map<string, int> c;
        for (auto cd : cpdomains){
            int i = cd.find(" ");
            int n = stoi(cd.substr(0, i));
            string s = cd.substr(i + 1, cd.size() - i - 1);
            for (int i = 0; i < s.size(); ++i)
                if (s[i] == '.') 
                    c[s.substr(i + 1, s.size () - i)] += n;
            c[s] += n;
        }
        vector<string> res;
        for (auto k : c) res.push_back(to_string(k.second) + " " + k.first);
        return res;
    }
};
class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        unordered_map<string, unsigned> imap;
        //其中string为子域,即在每一个字符串元素中由'.'来分分隔开;unsigned的值为子域出现的频次,不可能小于0,所以定义为无符号型。
        for(const string &cpdomain : cpdomains)
        //使用范围for循环遍历每一个字符串元素cpdomain,由于对cpdomain只进行读操作,所以定义为const string (auto)型。
        {
            size_t pos = cpdomain.find(' ');//找到第一个数字后面的空格
            string count = string(cpdomain.begin(), cpdomain.begin() + pos);
            //在每个字符串元素cpdomain中,子域出现的频次count在最前面且由空格隔开,所以如果找到空格所在的位置pos,则数字字符串位于begin()和begin() + pos之间;
            while(1){
                pos ++;
                imap[string(cpdomain.begin() + pos, cpdomain.end())] += stoi(count);//把对应的关键字映射到频次;stoi()函数把字符串转换为整型
                pos = cpdomain.find('.', pos);
                if(pos == string::npos) break;//找到'.'的位置,加1操作以后继续对下一个cpdomain进行处理,直到找不到'.';
            }
        }
        vector<string> res; //定义一个字符串型的顺序容器res;
        for(const auto &m : imap){
            //添加新元素到容器的末尾
            res.emplace_back(to_string(m.second));
            //使用vector的成员函数emplace_back()每次向res容器的末端添加一个字符串元素(值,使用to_string()把整型转换为字符串);
            res.back() += " ";//使用vector的成员函数back()返回容器中的最后一个字符
            res.back() += m.first; //使用'+'连接空格和关键字,
            //res.emplace_back(" ");
            //res.emplace_back(m.first);
        }
        return res;
    }
};
#include<string>
using namespace std;
class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        map<string, int> m;
        for(int i = 0; i < cpdomains.size(); i++){
            int index_ = cpdomains[i].find(' ');
            int count = stoi(cpdomains[i].substr(0, index_));
            string domain = cpdomains[i].substr(index_+1);
            m[domain] += count;
            while((index_ = domain.find(".")) != string::npos){
                domain = domain.substr(index_+1);
                m[domain] += count;
            }
        }
        vector<string> res;
        for(const auto& v : m){
            res.push_back( to_string(v.second) + " " + v.first);
        }
        return res;
    }
};

还不太懂这道题。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列。 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和't'都只出现一次。因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。 示例 2: 输入: "cccaaa" 输出: "cccaaa" 解释: 'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。注意"cacaca"是不正确的,因为相同的字母必须放在一起。 示例 3: 输入: "Aabb" 输出: "bbAa" 解释: 此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。注意'A'和'a'被认为是两种不同的字符。 Java代码如下: ``` import java.util.*; public class Solution { public String frequencySort(String s) { if (s == null || s.length() == 0) { return ""; } Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> entry : list) { char c = entry.getKey(); int count = entry.getValue(); for (int i = 0; i < count; i++) { sb.append(c); } } return sb.toString(); } } ``` 解题思路: 首先遍历字符串,使用HashMap记录每个字符出现的次数。然后将HashMap转换为List,并按照出现次数从大到小进行排序。最后遍历排序后的List,将每个字符按照出现次数依次添加到StringBuilder中,并返回StringBuilder的字符串形式。 时间复杂度:O(nlogn),其中n为字符串s的长度。遍历字符串的时间复杂度为O(n),HashMap和List的操作时间复杂度均为O(n),排序时间复杂度为O(nlogn),StringBuilder操作时间复杂度为O(n)。因此总时间复杂度为O(nlogn)。 空间复杂度:O(n),其中n为字符串s的长度。HashMap和List的空间复杂度均为O(n),StringBuilder的空间复杂度也为O(n)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值