每日刷题【day008】
作者:互联网
1、子数组最大平均数 I
public double findMaxAverage(int[] nums, int k) {
int sum=0;
for (int i=0;i<k;i++){
sum+=nums[i];
}
int maxSum=sum;
for (int i=k;i<nums.length;i++){
sum=sum-nums[i-k]+nums[i];
maxSum=Math.max(sum,maxSum);
}
return 1.0*maxSum/k;
}
2、移除元素
public int removeElement(int[] nums, int val) {
int res=0;
for (int i=0;i<nums.length;i++){
if (nums[i]!=val) nums[res++]=nums[i];
}
return res;
}
标签:val,nums,int,res,每日,maxSum,day008,sum,刷题 来源: https://blog.csdn.net/qq_53273362/article/details/113665106