子域名访问计数 + 数组的度

本文介绍了两个编程问题的解决方案。第一个问题是解析计数配对域名,将每个子域名的访问次数累加到其父域名,返回所有域名的访问次数列表。第二个问题是在给定整数数组中找到与数组度相同的最短连续子数组的长度,通过哈希表记录元素出现的频数和位置来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

子域名访问计数

题目:

网站域名 "discuss.leetcode.com" 由多个子域名组成。顶级域名为 "com" ,二级域名为 "leetcode.com" ,最低一级为 "discuss.leetcode.com" 。当访问域名 "discuss.leetcode.com" 时,同时也会隐式访问其父域名 "leetcode.com" 以及 "com" 。

计数配对域名 是遵循 "rep d1.d2.d3" 或 "rep d1.d2" 格式的一个域名表示,其中 rep 表示访问域名的次数,d1.d2.d3 为域名本身。

例如,"9001 discuss.leetcode.com" 就是一个 计数配对域名 ,表示 discuss.leetcode.com 被访问了 9001 次。

给你一个 计数配对域名 组成的数组 cpdomains ,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。

示例 1:

输入:cpdomains = ["9001 discuss.leetcode.com"]
输出:["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
解释:例子中仅包含一个网站域名:"discuss.leetcode.com"。
按照前文描述,子域名 "leetcode.com" 和 "com" 都会被访问,所以它们都被访问了 9001 次。

示例 2:

输入:cpdomains = ["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 次。

提示:

    1 <= cpdomain.length <= 100
    1 <= cpdomain[i].length <= 100
    cpdomain[i] 会遵循 "repi d1i.d2i.d3i" 或 "repi d1i.d2i" 格式
    repi 是范围 [1, 104] 内的一个整数
    d1i、d2i 和 d3i 由小写英文字母组成

class Solution {
    public List<String> subdomainVisits(String[] cpdomains) {
        List<String> result = new ArrayList<>();
        HashMap<String, Integer> tempMap = new HashMap<>();
        for (int i = 0; i < cpdomains.length; i++) {
            int cal = 0;  //用来存放当前域名的出现次数
            int len = cpdomains[i].length();
            for (int j = 0; j < len; j++) {
                if (cpdomains[i].charAt(j) == ' ') {
                    cal = Integer.parseInt(cpdomains[i].substring(0, j));  // 当前域名的出现次数
                    // 按照出现次数放入一级域名
                    String str = cpdomains[i].substring(j + 1, len);
                    if (tempMap.containsKey(str)) tempMap.put(str, tempMap.get(str) + cal);
                    else tempMap.put(str, cal);
                } else if (cpdomains[i].charAt(j) == '.') {
                    // 依次放入二级三级域名
                    String str = cpdomains[i].substring(j + 1, len);
                    if (tempMap.containsKey(str)) tempMap.put(str, tempMap.get(str) + cal);
                    else tempMap.put(str, cal);
                }
            }
        }
        for (String key : tempMap.keySet()) {
            result.add(tempMap.get(key) + " " + key);
        }
        return result;
    }
}

执行结果:

 

数组的度

题目:

给定一个非空且只包含非负数的整数数组 nums,数组的 度 的定义是指数组里任一元素出现频数的最大值。任务是在 nums 中找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。

示例 1:

输入:nums = [1,2,2,3,1]
输出:2
解释:
输入数组的度是 2 ,因为元素 1 和 2 的出现频数最大,均为 2 。
连续子数组里面拥有相同度的有如下所示:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
最短连续子数组 [2, 2] 的长度为 2 ,所以返回 2 。

示例 2:

输入:nums = [1,2,2,3,1,4,2]
输出:6
解释:
数组的度是 3 ,因为元素 2 重复出现 3 次。
所以 [2,2,3,1,4,2] 是最短子数组,因此返回 6 。

提示:

    nums.length 在 1 到 50,000 范围内。
    nums[i] 是一个在 0 到 49,999 范围内的整数。
 

class Solution {
    public int findShortestSubArray(int[] nums) {
    	/**
    	*哈希表
		*键存储数组不同的元素
		*值为长度为 3的数组
		*	第一位存储元素频数
		*	第二位存储元素第一次出现的下标
		*	第三位存储元素最后一次出现的下标
		*	
		*坐标之差加上 1即同度的最短连续子数组长度
		*/
        Map<Integer, int[]> myMap = new HashMap<>();
		for(int i = 0; i < nums.length; i++) {
			if(myMap.containsKey(nums[i])) {
				myMap.get(nums[i])[0]++;
				myMap.get(nums[i])[2] = i;
			} else
				myMap.put(nums[i], new int[] {1, i, i});
		}
		//频数均大于等于 1
		int maxFrequency = 0;
		int tempFrequency = 0;
		int subArrLen = 0;
		int tempSubArrLen = 0;
		Object[] ob = myMap.keySet().toArray();
		//遍历集合
		for(int i = 0; i < ob.length; i++) {
			tempFrequency = myMap.get(ob[i])[0];
			tempSubArrLen = myMap.get(ob[i])[2] - myMap.get(ob[i])[1] + 1;
			//获取更大频数对应的子数组长度
			if(tempFrequency > maxFrequency) {
				maxFrequency = tempFrequency;
				subArrLen = tempSubArrLen;
			//频数相等则获取对应的更小的子数组长度
			} else if(tempFrequency == maxFrequency)
				if(tempSubArrLen < subArrLen)
					subArrLen = tempSubArrLen;
		}
        return subArrLen;
    }
}

执行结果

 来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-submatrices-that-sum-to-target

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值