LeetCode Weekly Contest 84

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rsy56640/article/details/80302748

行策课没事干,和晨哥窜通一下,打打周赛暖暖手。感觉好久都没写代码了。。

832 Flipping an Image
题目链接:
https://leetcode.com/contest/weekly-contest-84/problems/flipping-an-image/

class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        const int size = A.size();
        if (size == 0)return A;
        const int length = A[0].size();
        const bool odd = length % 2;
        for (int i = 0; i < size; ++i)
        {
            for (int j = 0; j < length / 2; ++j)
            {
                if (A[i][j] == A[i][length - 1 - j])
                {
                    A[i][j] ^= 1;
                    A[i][length - 1 - j] ^= 1;
                }
            }
            if (odd)A[i][length / 2] ^= 1;
        }
        return A;
    }
};

833 Find And Replace in String
题目链接:
https://leetcode.com/contest/weekly-contest-84/problems/find-and-replace-in-string/

bool compare(const string& s, int size, int index, const string& source)
{
    const int length = source.size();
    const int len = index + length;
    if (len > size)return false;
    for (int i = index, j = 0; i < len; ++i, ++j)
        if (s[i] != source[j])return false;
    return true;
}

class Solution {
public:
    string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string>& targets) {
        const int size = indexes.size();
        const int s_size = S.size();
        if (size == 0)return S;
        using id = int;
        vector<pair<int, id>> v(size);
        for (int i = 0; i < size; ++i)
            v[i] = std::make_pair(indexes[i], i);
        std::sort(v.begin(), v.end(),
            [](const pair<int, id>& lhs, const pair<int, id>& rhs)->bool
        {return lhs.first < rhs.first; });
        string ans;
        if (indexes[0] != 0)
            ans.append(S, 0, indexes[v[0].second]);
        for (int i = 0; i < size; ++i)
        {
            int index = v[i].first;
            id _id = v[i].second;
            int next_index = (i == size - 1) ? s_size : v[i + 1].first;
            if (compare(S, s_size, index, sources[_id]))
            {
                ans.append(targets[_id]);
                int middle_index = index + sources[_id].size();
                ans.append(S, middle_index, next_index - middle_index);
            }
            else
                ans.append(S, index, next_index - index);
        }
        return ans;
    }
};

835 Image Overlap
题目链接:
https://leetcode.com/contest/weekly-contest-84/problems/image-overlap/

class Solution {
public:
    int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
        const int size = A.size();
        int largest_overlap = 0;
        for (int i = 0; i < size; ++i)
            for (int j = 0; j < size; ++j)
            {
                int count_A = 0;
                int count_B = 0;
                for (int p = i; p < size; ++p)
                    for (int q = j; q < size; ++q)
                    {
                        if (A[p - i][q - j] == 1 && B[p][q] == 1)count_A++;
                        if (B[p - i][q - j] == 1 && A[p][q] == 1)count_B++;
                    }
                largest_overlap = max(largest_overlap, max(count_A, count_B));
            }
        return largest_overlap;
    }
};

834 Sum of Distances in Tree
题目链接:
https://leetcode.com/contest/weekly-contest-84/problems/sum-of-distances-in-tree/

class Solution {
private:
    /*
     * in dfs
     * count[i] means the amount of the subtree[i]
     * ans[i] means the total distances from the subtree[i] to the subroot[i].
    **/
    void dfs(int root, bool* visit, const vector<vector<int>>& nodes, vector<int>& count, vector<int>& ans)
    {
        visit[root] = true;
        for (auto it = nodes[root].begin(); it != nodes[root].end(); ++it)
            if (!visit[*it])
            {
                dfs(*it, visit, nodes, count, ans);
                count[root] += count[*it];
                ans[root] += count[*it] + ans[*it];
            }
        count[root] += 1;
    }
    /*
     * in calculate
     * ans is to be calculated as the real result.
     * for each subtree, its father has been calculated properly,
     * and ans[i] has a little change on ans[root],
     * count[i] nodes is closer from the subroot, and (N - count[i]) is away from the subroot.
     * then recur.
    **/
    void calculate(int root, int N, bool* visit, const vector<vector<int>>& nodes, const vector<int>& count, vector<int>& ans)
    {
        visit[root] = true;
        for (auto it = nodes[root].begin(); it != nodes[root].end(); ++it)
            if (!visit[*it])
            {
                ans[*it] = ans[root] - count[*it] + (N - count[*it]);
                calculate(*it, N, visit, nodes, count, ans);
            }
    }

public:
    vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
        vector<vector<int>> nodes(N);
        for (int i = 0; i < N - 1; ++i)
        {
            nodes[edges[i][0]].push_back(edges[i][1]);
            nodes[edges[i][1]].push_back(edges[i][0]);
        }
        vector<int> ans(N, 0);
        vector<int> count(N, 0);      //count[i] represents the amount in the subtree[i]
        bool* visit = new bool[N];
        for_each(visit, visit + N, [](bool& b) {b = false; });
        dfs(0, visit, nodes, count, ans);
        for_each(visit, visit + N, [](bool& b) {b = false; });
        calculate(0, N, visit, nodes, count, ans);
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/rsy56640/article/details/80302748