LeetCode 199场周赛


class Solution {
public:
    
    string restoreString(string s, vector<int>& indices) {
        char temp[105] = "";
        string ans;
        for (int i =0; i < s.length(); i++){
            temp[indices[i]] = s[i];
            
        }
        
        for (int i = 0; i < s.length(); i++){
            ans += temp[i];
        }
        return ans;
    }
};

class Solution {
public:
    int minFlips(string target) {
        int cnt = 0;
        int ans = 0;
        while(target[cnt] == '0') cnt++;
        
        for(int i = cnt ; i < target.length(); i++) {
            if (target[i] == '1') {
                ans += 1;
                while(target[i + 1] == '1') i++;
            } else {
                ans += 1;
                while(target[i + 1] == '0') i++;
            }
        }
        return ans;
    }
};

const int MAXN = 1050 + 50;
int cnt[MAXN][15], tot, ans;
class Solution {
public:

    int dfs(TreeNode* rt, int D){
        int x = ++tot;
        int l = 0, r = 0;
        if (rt->left) l = dfs(rt->left, D);
        if (rt->right) r = dfs(rt->right, D);
        if (l > 0 && r > 0){
            for (int i = 0; i <= D; i++)
                for (int j = 0; j <= D; j++)
                    if (i + j + 2 <= D) ans += cnt[l][i] * cnt[r][j];
        }
        for (int i = 0; i < D; i++)
            cnt[x][i + 1] = cnt[l][i] + cnt[r][i];
        if (l == 0 && r == 0) cnt[x][0] = 1;
        return x;
    }

    int countPairs(TreeNode* root, int D) {
        ans = tot = 0; memset(cnt, 0, sizeof(cnt));
        dfs(root, D);
        return ans;
    }
};


posted @ 2020-08-03 20:22  DengSchoo  阅读(127)  评论(0编辑  收藏  举报