【两次过】Lintcode 1006. 子域名访问计数

诸如discuss.lintcode.com这样的域名由各种子域名构成。最顶层是com,下一层是lintcode.com,最底层是discuss.lintcode.com.当访问discuss.lintcode.com时,会隐式访问子域名lintcode.comcom.

现给出域名的访问计数格式为“空格 地址”。 示例:9001 discuss.lintcode.com.

给出计数列表cpdomains. 返回每个子域名(包含父域名)的访问次数(与输入格式相同,顺序任意).

样例

样例 1:

输入: 
["9001 discuss.lintcode.com"]
输出: 
["9001 discuss.lintcode.com", "9001 lintcode.com", "9001 com"]
解释: 
只有一个域名:"discuss.lintcode.com". 如题所述,  
子域名"lintcode.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"]
解释:
一共访问900次"google.mail.com",50次"yahoo.com",1次"intel.mail.com",5次"wiki.org".   
对于所有的子域名,会访问 900 + 1 = 901 次"mail.com",900 + 50 + 1 = 951 次"com",5次"org".

注意事项

  • cpdomains的长度不超过100.
  • 每个域名的长度不超过100.
  • 会有1到2个.包含在每个域名中.
  • 任何域名的访问计数都不会超过10000.

返回结果的条目顺序随意.


解题思路:

首先将字符串的用空格分隔开,分为数字与域名两部分,然后将域名再分解为子域名,最后将数字与各级子域名放入Map中,达到一种映射关系,再依次遍历下一个字符串,如果map中包含了此key则将value值更新,否则加入key和value。最后将map中的元素按一定格式放入list中返回即可。

public class Solution {
    /**
     * @param cpdomains: a list cpdomains of count-paired domains
     * @return: a list of count-paired domains
     */
    public List<String> subdomainVisits(String[] cpdomains) {
        // Write your code here
        Map<String,Integer> map = new HashMap<>();//key为子域名字符串,value为访问次数

        for(int i=0 ; i<cpdomains.length ; i++){
            String[] temp = cpdomains[i].split(" ");//temp[0]为次数,temp[1]为域名字符串
            
            //将最底层域名与次数存进map中
            if(map.get(temp[1]) == null)
                map.put(temp[1] , Integer.valueOf(temp[0]));
            else
                map.put(temp[1] , Integer.valueOf(temp[0]) + map.get(temp[1]));

            int lastComma = temp[1].lastIndexOf(".");
            int firstComma = temp[1].indexOf(".");

            //将最顶层域名与次数存进map中
            if(map.get(temp[1].substring(lastComma+1)) == null)
                map.put(temp[1].substring(lastComma+1) , Integer.valueOf(temp[0]));
            else
                map.put(temp[1].substring(lastComma+1) , Integer.valueOf(temp[0]) + map.get(temp[1].substring(lastComma+1)));
            
            //如果存在次级域名,则将次级域名与次数存进map中
            if(lastComma != firstComma){
                if(map.get(temp[1].substring(firstComma+1)) == null)
                    map.put(temp[1].substring(firstComma+1) , Integer.valueOf(temp[0]));
                else
                    map.put(temp[1].substring(firstComma+1) , Integer.valueOf(temp[0]) + map.get(temp[1].substring(firstComma+1)));
            }
        }

        List<String> list = new ArrayList<>();
        
        //将map中存放的数据依次存入list中
        for(String s : map.keySet())
            list.add(map.get(s) + " " + s);

        return list;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值