Leetcode周赛201

A 1544. 整理字符串

思路:
直接模拟,题意坑爹。

class Solution {
     
public:
    string makeGood(string s) {
     
        int len = s.length();
        vector<char>ans;
        
        for(int i = 0;i < len;i++) {
     
            ans.push_back(s[i]);    
        }
        
        for(int i = 1;i <= 100;i++) {
     
            vector<char>tmp;
            for(int j = 0;j < ans.size();j++) {
     
                if(j < ans.size() - 1 && ans[j] <= 'z' && ans[j] >= 'a' && ans[j + 1] <= 'Z' && ans[j + 1] >= 'A' && ans[j] - 'a' == ans[j + 1] - 'A') {
     
                    j ++;
                } else if(j < ans.size() - 1 && ans[j] <= 'Z' && ans[j] >= 'A' && ans[j + 1] <= 'z' && ans[j + 1] >= 'a' && ans[j] - 'A' == ans[j + 1] - 'a') {
     
                    j++;
                } else {
     
                    tmp.push_back(ans[j]);
                }
            }   
            ans = tmp;
        }
        string str;
        for(int i = 0;i < ans.size();i++) str += ans[i];
        return str;
    }
};

B.1545. 找出第 N 个二进制字符串中的第 K 位(递归)

class Solution {
     
public:
    string dfs(int l) {
     
        if(l == 1) return "0";
        string now = dfs(l - 1);
        string inv = now;
        for(int i = 0;i < inv.length();i++) {
     
            if(inv[i] == '0') inv[i] = '1';
            else inv[i] = '0';
        }
        reverse(inv.begin(),inv.end());
        return now + "1" + inv;
    }
    
    char findKthBit(int n, int k) {
     
        string ans = dfs(n);
        return ans[k - 1];
    }
};

C.1546. 和为目标值的最大数目不重叠非空子数组数目(map)

直接用map存前缀和,杭电多校原题hhh,不过那个求的是target倍数。

class Solution {
     
public:
    map<int,int>mp;
    int maxNonOverlapping(vector<int>& nums, int target) {
     
        int len = nums.size();
        int now = 0,ans = 0;
        mp[0] = 1;
        for(int i = 0;i < len;i++) {
     
            now = (now + nums[i]);
            if(mp[now - target]) {
     
                ans++;
                mp.clear();
                now = 0;
                mp[0] = 1;
            } else {
     
                mp[now] = 1;
            }
        }
        return ans;
    }
};

dp也可以写

class Solution {
     
public:
    int dp[100005];
    map<int,int>last;
    int maxNonOverlapping(vector<int>& nums, int target) {
     
        int now = 0;
        dp[0] = 0;
        last[0] = 1;
        int n = nums.size();
        for(int i = 0;i < n;i++) {
     
            now += nums[i];
            dp[i + 1] = dp[i];
            if(last[now - target] != 0) {
     
                dp[i + 1] = max(dp[i + 1],dp[last[now - target] - 1] + 1);
            }
            last[now] = i + 2;
        }
        return dp[n];
    }
};

D.1547. 切棍子的最小成本(区间DP)

思路:
裸的石子合并。
谁能想到1e6也能区间DP呢(虚假)?

class Solution {
     
public:
    int f[105][105];
    
    int minCost(int n, vector<int>& cuts) {
     
        sort(cuts.begin(),cuts.end());
        
        vector<int>num;
        num.push_back(0);
        for(int i = 0;i < cuts.size();i++) {
     
            num.push_back(cuts[i]);
        }
        num.push_back(n);
        memset(f,0x3f,sizeof(f));
        int len = num.size();
        for(int i = 0;i < len - 1;i++) {
     
            f[i][i + 1] = 0;
        }
        for(int i = 0;i < len;i++) f[i][i] = 0;
        // printf("FUCK %d %d\n",f[0][1],f[1][2]);
        for(int l = 3;l <= len;l++) {
     
            for(int i = 0;i + l - 1 < len;i++) {
     
                int j = i + l - 1;
                for(int k = i + 1;k < j;k++) {
     
                    f[i][j] = min(f[i][j],f[i][k] + f[k][j] + num[j] - num[i]);
                    // printf("%d %d %d\n",i,j,k);
                }
            }
        }
        return f[0][len - 1];
    }
};

你可能感兴趣的:(#,leetcode)