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;
}
2156

被折叠的 条评论
为什么被折叠?



