1. 程式人生 > >LeetCode Weekly Contest 110 (C++)

LeetCode Weekly Contest 110 (C++)

Welcome to the 110th LeetCode Weekly Contest

937. Reorder Log Files

You have an array of logs.  Each log is a space delimited string of words.

For each log, the first word in each log is an alphanumeric identifier.  Then, either:

  • Each word after the identifier will consist only of lowercase letters, or;
  • Each word after the identifier will consist only of digits.

We will call these two varieties of logs letter-logs and digit-logs.  It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log(讓所有字母log出現在數字log前邊).  The letter-logs are ordered lexicographically(字典序) ignoring identifier, with the identifier used in case of ties.  The digit-logs should be put in their original order.(字母log按照字典序排序,數字log相對順序不變)

Return the final order of the logs.

Input:    [ "a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo" ]

Output: [ "g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7" ]

// 為了按照字典序排序
bool cmp(string& l, string& r)
{
    int lp = l.find(" ");
    int rp = r.find(" ");
    return l.substr(lp) < r.substr(rp);
}
vector<string> reorderLogFiles(vector<string>& logs)
{
    int d = logs.size() - 1, l = d;     
    while (l >= 0)
    {
        if (isdigit(logs[d].back()))
        {
            --d;
            l = d - 1;
        }
        else if (isalpha(logs[l].back()))
            --l;
        else
        {
            string tmp = logs[d];
            logs[d] = logs[l];
            logs[l] = tmp;
        }
    }
    sort(logs.begin(), logs.begin() + d + 1, cmp);
    return logs;
}

938. Range Sum of BST

Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).

The binary search tree is guaranteed to have unique values.

Input:  root = [ 10, 5, 15, 3, 7, null, 18 ], L = 7, R = 15                Output: 32

Input:  root = [ 10, 5, 15, 3, 7, 13, 18, 1, null, 6 ], L = 6, R = 10  Output: 23

int dfs(TreeNode* nod, const int& L, const int& R)
{
   return ((nod->val >= L && nod->val <= R) ? nod->val : 0)
          + (nod->left == NULL  ? 0 : dfs(nod->left, L, R)) 
          + (nod->right == NULL ? 0 : dfs(nod->right, L, R));
}
int rangeSumBST(TreeNode* root, int L, int R) {
    if(!root) return 0;
    return dfs(root, L, R);
}

939. Minimum Area Rectangle

Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.

If there isn't any rectangle, return 0.

Input: [ [1, 1], [1, 3], [3, 1], [3, 3], [2, 2] ]            Output: 4

Input: [ [1, 1], [1, 3], [3, 1], [3, 3], [4, 1], [4, 3] ]  Output: 2

解:

    用 set<pair<int, int>>  來儲存所有點的座標,然後對於每兩個點,如果存在和他們組成四邊形的另兩個點,那就能夠組成矩形,記錄面積,迴圈找出最小面積。

int minAreaRect(vector<vector<int>>& points) {
    bool found = false;
    int psize = points.size(), min_area = INT_MAX;
    set<pair<int, int>> setp;	// unordered_set會報錯 "The C++ Standard doesn't provide a hash for this type."
    for (auto p : points)
        setp.insert(pair<int, int>(p[0], p[1]));
    for (int i = 0; i < psize; i++)
        for (int j = i + 1; j < psize; j++)
        {
            int x1 = points[i][0], y1 = points[i][1], x2 = points[j][0], y2 = points[j][1];
            if (x1 == x2 || y1 == y2) continue;
            if (abs(x1 - x2) * abs(y1 - y2) >= min_area) continue;	// 一定不是最小矩形
            if (setp.find(pair<int, int>(x1, y2)) != setp.end()
             && setp.find(pair<int, int>(x2, y1)) != setp.end())	// 另兩個點存在
            {
                found = true;
                min_area = min(min_area, abs(x1 - x2) * abs(y1 - y2));
            }
        }
    if (found == false) return 0;
    return min_area;
}

940. Distinct Subsequences II

Given a string S, count the number of distinct, non-empty subsequences of S .

Since the result may be large, return the answer modulo 10^9 + 7.

Input: "abc"
Output: 7
Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".

Input: "aba"
Output: 6
Explanation: The 6 distinct subsequences are "a", "b", "ab", "ba", "aa" and "aba".

Input: "aaa"
Output: 3
Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".

解:

    "abc" 的情況沒有 "ca" 也就是必須是原來的順序。用一個26大小的陣列儲存以每個字母為結尾的單詞的個數,每次遍歷到一個字母的時候,比如 'a',那麼以 a 為結尾的單詞數是整個陣列所有數的和 + 1,加1的意思就是 "a" 這個字串,原來如果有 "a" 也變成了 "aa" 所以沒有問題。 

int distinctSubseqII(string S) {
    long endsWith[26] = {}, mod = 1e9 + 7;
    for (char c : S)
        endsWith[c - 'a'] = accumulate(begin(endsWith), end(endsWith), (long int)1) % mod;
    return accumulate(begin(endsWith), end(endsWith), 0L) % mod;
}