- 第一次参加 我激动的心 颤抖的手 勉勉强强提交了第一题 磕磕绊绊到达并最终倒在了第二题>-<
[6229. 对数组执行操作]
class Solution {
public:
vector<int> applyOperations(vector<int>& nums) {
int length = nums.size();
vector<int> result (length , 0);
int j =0;
int i =0;
if(length <= 2){
while(i <= 1){
if (nums[i] != 0){
result[j] = nums[i];
j++;
}
i++;
}
return result;
}
for (i = 0; i <= length-2; i++){
if (nums[i] == nums[i+1]){
nums[i] = nums[i] *2;
nums[i+1] = 0;
}
if (nums[i] != 0){
result[j] = nums[i];
j++;
}
}
if (nums[length-1] != 0)
result[j] = nums[length-1];
return result;
}
};
- 看看第一名 1分31秒
class Solution {
public:
vector<int> applyOperations(vector<int>& nums) {
vector<int> a(nums),ans;
int n=a.size(),i;
for(i=0;i+1<n;i++)if(a[i]==a[i+1])
{
a[i]<<=1;
a[i+1]=0;
}
for(i=0;i<n;i++)if(a[i])ans.push_back(a[i]);
for(i=0;i<n;i++)if(!a[i])ans.push_back(a[i]);
return ans;
}
};
[6230. 长度为 K 子数组中的最大和]
- 第二题先抄上来 明天看看自己当时卡哪了
class Solution {
typedef long long ll;
int a[100005],pr[100005];
ll s[100005];
public:
long long maximumSubarraySum(vector<int>& nums, int k) {
memset(a,-1,sizeof(a));
int n=nums.size(),i;
for(pr[0]=-1,a[nums[0]]=0,i=1;i<n;i++)
{
pr[i]=max(pr[i-1],a[nums[i]]);
a[nums[i]]=i;
}
long long ans=0;
for(i=0;i<n;i++)s[i+1]=s[i]+nums[i];
for(i=k-1;i<n;i++)if(pr[i]<=i-k)ans=max(ans,s[i+1]-s[i-k+1]);
return ans;
}
};
呜呜呜 完败 明天继续:)
标签:周赛,318,lc,nums,int,long,vector,public,size From: https://www.cnblogs.com/deservee/p/16864148.html