【20221224】每日一题

class Solution {
public:
    string largestMerge(string word1, string word2) {
        string ans;
        int i = 0, j = 0;
        while (i < word1.size() || j < word2.size()) {
            if (i < word1.size() && word1.substr(i) > word2.substr(j)) {
                ans += word1[i++];
            } else { // i > word1.size() || word1.substr(i) > word2.substr(j)
                ans += word2[j++];
            }
        }

        return ans;
    }
};

贪心 + 双指针

posted @ 2022-12-24 10:25  zhanghanLeo  阅读(27)  评论(0)    收藏  举报