leetcode第355场周赛补题

6921. 按分隔符拆分字符串 - 力扣(LeetCode)

思路:模拟

class Solution {
public:
    vector<string> splitWordsBySeparator(vector<string>& words, char s) {
        vector<string> res;
        string tmp;
        for(auto t : words)
        {
            int i = 0;
            while(t[i] == s) i ++ ;
            for( ; i < t.size(); i ++ )
            {
                while (t[i] == s && tmp.size() == 0) i ++ ;
                if(t[i] == s && tmp.size())
                {
                    res.push_back(tmp);
                    tmp.erase(0, tmp.size());
                    continue ;
                }
                if(t[i] != s && i < t.size()) tmp += t[i];
            }
            if(tmp.size())
            {
                res.push_back(tmp);
                tmp.erase(0, tmp.size());
            }
        }
        return res;
    }
};

6915. 合并后数组中的最大元素 - 力扣(LeetCode)

思路:贪心,从后往前模拟

class Solution {
public:
    long long maxArrayValue(vector<int>& nums) {
        int n = nums.size();
        long long res = 0;
        if(n == 1) return nums[0];
        if(n == 2)
        {
            if(nums[0] > nums[1]) return nums[0];
            else return (long long)nums[0] + nums[1];
        }
        long long tmp = nums[n - 1];
        for(int i = n - 2; i >= 0; i -- )
        {
            if(nums[i] <= tmp)
            {
                tmp += nums[i];
                res = max(tmp, res);
            }
            else
            {
                res = max(res, (long long) nums[i]);
                tmp = nums[i];
            }
        }
        return res;
    }
};

6955. 长度递增组的最大数目 - 力扣(LeetCode)

思路:贪心

class Solution {
public:
    int maxIncreasingGroups(vector<int>& usageLimits) {
        int  n = usageLimits.size();
        vector<long long> num(usageLimits.begin(), usageLimits.end());
        sort(num.begin(), num.end());

        int x = 1;
        for(int i = 0; i < n; i ++ )
        {
            if(num[i] >= x) num[i] -= x ++ ;
            if(i < n - 1) num[i + 1] +=  num[i]; 
        }

        return x - 1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值