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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值