zoukankan      html  css  js  c++  java
  • 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;
        }
    };
    
    

  • 相关阅读:
    集合set
    字典
    元组
    列表
    for循环
    Windows调试2.异常产生详细流程
    双机环境搭建
    Windows调试1.WinDbg基本使用-异常基础知识
    PE基础7-HOOK练习
    PE基础6_远程线程注入-HOOK(消息-InLine-IAT)
  • 原文地址:https://www.cnblogs.com/DengSchoo/p/13428428.html
Copyright © 2011-2022 走看看