Day 13

1.牛牛冲砖五 (pass)

牛牛冲砖五

1.1 解析

在这里插入图片描述

1.2 代码

#include <iostream>
#include <string>
using namespace std;
int t,n,k;
string s;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k>>s;
        int ret=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='L')ret--;
            else
            {
                if(i>=2&&s[i-1]=='W'&&s[i-2]=='W')
                    ret+=k;
                else ret++;
            }
        }
        cout<<ret<<endl;
    }
    return 0;
}

2.最长无重复子数组 (pass)

最长无重复子数组
滑动窗口、双指针

2.1 解析

在这里插入图片描述

2.2 代码

class Solution {
    int Hash[100010]={0};
public:
    int maxLength(vector<int>& arr) {
        int n=arr.size();
        int left=0,right=0,ret=0;
        while(right<n)
        {
            Hash[arr[right]]++;//进窗口
            while(Hash[arr[right]]>=2)//判断
            {
                left++;
                Hash[arr[left-1]]--;//出窗口
            }
            //更新结果
            if(ret<right-left+1)
                ret=right-left+1;
            right++;
        }
        return ret;
    }
};

3.重排字符串

重排字符串
贪心

3.1 解析

在这里插入图片描述

3.2 代码

#include <iostream>
#include <string>
using namespace std;
const int N=1e5+10;
char str[N]={0};
char ret[N]={0};
int main()
{
    int n=0;
    cin>>n;
    for(int i=0;i<n;i++) cin>>str[i];
    
    //1.找出现次数最多的字符
    int maxcount=0;
    char maxch='0';
    int hash[26]={0};
    for(int i=0;i<n;i++)
    {
        hash[str[i]-'a']++;
        if(maxcount<hash[str[i]-'a'])
        {
            maxcount=hash[str[i]-'a'];
            maxch=str[i];
        }
    }
    //2.看是否能重排
    if(maxcount>(n+1)/2) cout<<"no"<<endl;
    else 
    {
        cout<<"yes"<<endl;
        int i=0;
        //1.优先处理出现次数最多的字符
        while(maxcount--)
        {
            ret[i]=maxch;
            i+=2;
        }
        //2.优先处理相同的字符
        for(int j=0;j<26;j++)
        {
            if(hash[j]&&j+'a'!=maxch)
            {
                while(hash[j]--)
                {
                    if(i>=n)i=1;
                    ret[i]=j+'a';
                    i+=2;
                }
            }
        }
        //3.输出
        for(int k=0;k<n;k++)cout<<ret[k];
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值