白菜刷LeetCode记-811.Subdomain Visit Count

好久没有写LeetCode,所以说坚持真的是一件很难的事情啊。今日重新开始吧,先来一道简单的题目,如下:

这道题首先想到的还是使用Map,代码如下:

/**
 * @param {string[]} cpdomains
 * @return {string[]}
 */
var subdomainVisits = function(cpdomains) {
    let tmp = new Map();
    let res = new Array();

    for(let i = 0 ; i < cpdomains.length ; i++){
        let tmparr = cpdomains[i].split(‘ ‘);
        if(tmp.has(tmparr[1])){
            tmp.set(tmparr[1], tmp.get(tmparr[1]) + parseInt(tmparr[0]));
        }else{
            tmp.set(tmparr[1], parseInt(tmparr[0]));
        }

        while(tmparr[1].indexOf(‘.‘) != -1){
            let tmpstr = tmparr[1].substring(tmparr[1].indexOf(‘.‘) + 1, tmparr[1].length);
            if(tmp.has(tmpstr)){
                tmp.set(tmpstr, tmp.get(tmpstr) + parseInt(tmparr[0]));
            }else{
                tmp.set(tmpstr, parseInt(tmparr[0]));
            }

            tmparr[1] = tmpstr;
        }
    }

    tmp.forEach((val, key) => res.push(val + " " + key));

    return res;

};

END

原文地址:https://www.cnblogs.com/sssysukww/p/9910992.html

时间: 2024-10-09 05:37:56

白菜刷LeetCode记-811.Subdomain Visit Count的相关文章

811.&#160;Subdomain Visit Count - LeetCode

Question 811.?Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"] Explanation: We only have one website domain: "discuss.

[LeetCode&amp;Python] Problem 811. Subdomain Visit Count

A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit

LeetCode 811 Subdomain Visit Count 解题报告

题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we

811. Subdomain Visit Count

题目描述: A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we

811. Subdomain Visit Count (5月23日)

解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains) { vector<string> result; map<string,int> pair; for(string str:cpdomains){ auto space=str.find(' '); int temp=stoi(str.substr(0,space)); str=s

白菜刷LeetCode记-328. Odd Even Linked List

发现简单题越来越少了,想偷懒都不可以了,今天的题目是中等难度的题目,题目如下: 这个题目是要根据链表的位置来修改链表,位置为奇数的节点全部排到前面,位置为偶数的节点全部排到奇数的后面,并且保持顺序不变. 想到的解决步骤为: 1.遍历数组,奇数的位置的节点组成一条新链表,偶数位置的节点组成另一个新链表: 2.将偶数链表接在奇数链表后面. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4

白菜刷LeetCode记-46. Permutations

今天这一题也是中等难度,题目如下: 这一题是要实现数组的全排列.这一题是要使用遍历以及递归的思想去实现,代码如下: 1 /** 2 * @param {number[]} nums 3 * @return {number[][]} 4 */ 5 var permute = function(nums) { 6 var res = new Array(); 7 8 helper(nums, 0, nums.length - 1 , res); 9 10 return res; 11 }; 12 1

白菜刷LeetCode记-122. Best Time to Buy and Sell Stock II

今天题目如下: 要求出最大的利益.这题个人不太想得通,看了答案也不太知道为什么这样能获得最大值.代码如下: 1 /** 2 * @param {number[]} prices 3 * @return {number} 4 */ 5 var maxProfit = function(prices) { 6 let maxp = 0; 7 for(let i = 0 ; i < prices.length ; i++){ 8 if(prices[i-1] < prices[i]){ 9 maxp

白菜刷LeetCode记-384. Shuffle an Array

今天早上是一道中等难度的题目,考的是洗牌算法. 个人对洗牌算法还是比较不熟悉的,因此是看答案的.参考链接为:https://www.jianshu.com/p/44100741cef5 基本思路为: 1) 将第一个元素与 n 个元素中的任意一个交换: 2) 将第二个与 n - 1 个元素进行交换: 3) 重复上述步骤,直到剩下1个元素. 代码如下: 1 var original; 2 var copy; 3 var num; 4 /** 5 * @param {number[]} nums 6