Skip to content
Xupan Luo edited this page Sep 22, 2018 · 6 revisions

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 a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.

Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".

We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

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.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.

Example 2:

Input:

["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]

Output:

["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]

Explanation:

We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.

/**
 * @param {string[]} cpdomains
 * @return {string[]}
 */
var subdomainVisits = function(cpdomains) {
    let ret = [];
    let map = {};
    cpdomains.forEach((item) => {
        let [port, domain] = item.split(' ');
        let array = domain.split('.');
        let len = array.length;
        let i = 0;
        while (i < len) {
            addParamToMap(map, array.slice(i, len).join('.'), port);
            i++;
        }
    });
    Object.keys(map).forEach((item) => {
        ret.push(`${map[item]} ${item}`);
    });
    return ret;
};

var addParamToMap = function(map, key, val) {
    if (map.hasOwnProperty(key)) {
        map[key] += +val;
    } else {
        map[key] = +val;
    }
}

Jewels and Stones

题目描述

You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example 1:

Input: J = "aA", S = "aAAbbbb"

Output: 3

Example 2:

Input: J = "z", S = "ZZ"

Output: 0

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.
class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int ret = 0;
        int lenJ = J.size();
        int lenS = S.size();
        for (int i = 0; i < lenJ; ++i) {
            for (int j = 0; j < lenS; ++j) {
                if (J[i] == S[j]) {
                    ret++;
                }
            }
        }
        return ret;
    }
};

优化解法~

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int ret = 0;
        map<char,int> strmap;
        for (auto i : J) {
            strmap[i] = 1;
        }
        for (auto i : S) {
            if (strmap.find(i) != strmap.end()) {
                ret++;
            }
        }
        return ret;
    }
};

To Lower Case

题目描述

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:

Input: "Hello"

Output: "hello"

Example 2:

Input: "here"

Output: "here"

class Solution {
public:
    string toLowerCase(string str) {
        string lowerstr = "";
        for (auto c : str) {
            if (isupper(c) != 0) {
                lowerstr += tolower(c);
            } else {
                lowerstr += c;
            }
        }
        return lowerstr;
    }
};

Sort Array By Parity

题目描述

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]

Output: [2,4,3,1]

The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        int left = 0;
        int right = A.size() - 1;
        while (left < right) {
            while (A[left] % 2 == 0 && left < right) {
                left++;
            }
            while (A[right] % 2 != 0 && left < right) {
                right--;
            }
            if (left < right) {
                int n = A[left];
                A[left] = A[right];
                A[right] = n;
            }
        }
        return A;
    }
};
Clone this wiki locally