leetcode 229 周赛

5685

给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。

返回 合并后的字符串 。
输入:word1 = "abc", word2 = "pqr"
输出:"apbqcr"

思路

  • 通过up进行判断
class Solution {
public:
    string mergeAlternately(string word1, string word2) {
        int i = 0, j = 0;
        string res;
        bool up = true;
        while (i <= word1.size() && j <= word2.size()) {
            if (i < word1.size() && up == true) {
                res += word1[i++];
                up = false;
            } else if (j < word2.size() && up == false) {
                res += word2[j++];
                up = true;
            }
            
            if (i == word1.size() && j == word2.size()) {
                break;
            } else if (i == word1.size() && j < word2.size()) {
                while (j < word2.size()) {
                    res += word2[j++];
                }
            } else if (j == word2.size() && i < word1.size()) {
                while (i < word1.size()) {
                    res += word1[i++];
                }
            }
        }
        return res;
    }
    
};

5686 移动所有球到每个盒子所需的最小操作数

vector minOperations(string boxes) {
        vector res;
        int cnt = 0;
        for (int i = 0; i < boxes.size(); i++) {
            for (int j = 0; j < boxes.size(); j++) {
                int tmp = boxes[j] - '0';
                if (tmp == 1) {
                    cnt += abs(j-i);
                }
            }
            res.push_back(cnt);
            cnt = 0;
        }

        return res;
    }

5867 执行乘法运算的最大分数

class Solution {
public:
    int maximumScore(vector& ns, vector& ms) {
        int m = ms.size();
        int n = ns.size();
        vector> mem(m + 1, vector(m + 1));
        mem[0][0] = 0;
        for (int i=1; i<=m; ++i) {
            mem[i][0] = mem[i-1][0] + ns[i-1] * ms[i-1]; //从前面选
            mem[0][i] = mem[0][i-1] + ns[n-i] * ms[i-1]; //从后面选
        }
        for (int i=1; i<=m; ++i) {
            for (int j=1; i+j<=m; ++j) {
                mem[i][j] = max(
                    mem[i-1][j]+ms[i+j-1]*ns[i-1], //从前面选
                    mem[i][j-1]+ms[i+j-1]*ns[n-j] //从后面选
                );
            }
        }
        int res = INT_MIN;
        for (int i=0; i<=m; ++i)
            res = max(res, mem[i][m-i]); //从前面选择了i个,从后面选择了m-i个
        return res;
    }
};

5678 由子序列构造的最长回文串的长度

class Solution {
public:
    int longestPalindrome(string word1, string word2) {
        //首尾分别在word1和word2中
        string word3 = word1 + word2;
        int mid = word1.size();
        return getString(word3, mid);
    }

    int getString(string s, int mid) {
        int n = s.size();
        int ans = 0;
        vector> dp(n, vector(n));
        for (int i = 0; i < n; i++) {
            dp[i][i] = 1;
        }
        for (int i = n-1; i >= 0; i--) {
            for (int j = i+1; j < n; j++) {
                if (s[i] == s[j]) {
                    dp[i][j] = dp[i+1][j-1] + 2;
                    if (i < mid && j >= mid) {//首尾在两个不同的字符串
                        ans = max(ans, dp[i][j]);
                    }
                } else {
                    dp[i][j] = max(dp[i+1][j],dp[i][j-1]);
                }
            }
        }
        return ans;
    }
};

你可能感兴趣的:(c++dpleetcode)