1. static int wing=[]()
  2. {
  3. std::ios::sync_with_stdio(false);
  4. cin.tie(NULL);
  5. return ;
  6. }();
  7.  
  8. class Solution
  9. {
  10. public:
  11. double findMaxAverage(vector<int>& nums, int k)
  12. {
  13. int sz=nums.size();
  14. double maxsum=0.0;
  15. double cursum=0.0;
  16. for(int i=;i<k;i++)
  17. cursum+=nums[i];
  18. maxsum=cursum;
  19. for(int j=k;j<sz;j++)
  20. {
  21. cursum=cursum-nums[j-k]+nums[j];
  22. maxsum=max(cursum,maxsum);
  23. }
  24. return maxsum/k;
  25. }
  26. };

扫描一遍,更新当前和,最大和,最后取走最大和即可

643. Maximum Average Subarray I的更多相关文章

  1. 【Leetcode_easy】643. Maximum Average Subarray I

    problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...

  2. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

  3. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  4. 643. Maximum Average Subarray

    Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...

  5. [Leetcode]643. Maximum Average Subarray I

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  6. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  7. leetcode 643. Maximum Average Subarray I 子数组最大平均数 I

    一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...

  8. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  9. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

随机推荐

  1. spring data jpa 注解

    @Data 注解引出的 lombok 小辣椒   今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok, ...

  2. JAVA Spring 事物 ( 已转账为例 ) 基于 AOP 注解

    <一> 配置为文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  3. as3 Function 中的call与apply方法

    apply方法,作用跟call一样,也可以用来改变函数执行时,this指针的指向,区别在于apply方法要求第二个参数必须是数组形式 例子,舞台上添加名为a和b的实例mc 案例1: a.getNumV ...

  4. jenkins 没有maven选项,怎么办

    第一步: 进入jenkins,点系统管理 第二:插件管理 点击“可选插件”  然后在右边的过滤输入框中输入搜索关键字: Maven Integration  或者 Pipeline Maven Int ...

  5. Python使用Threading模块创建线程

    使用Threading模块创建线程,直接从threading.Thread继承,然后重写__init__方法和run方法: #!/usr/bin/python # -*- coding: UTF-8 ...

  6. %s %r 区别 转

    也可说是 str() 和 repr() 的区别 转自:http://blog.csdn.net/wusuopubupt/article/details/23678291 %r用rper()方法处理对象 ...

  7. Web标准:八、下拉及多级弹出菜单

    Web标准:八.下拉及多级弹出菜单 知识点: 1.带下拉子菜单的导航菜单 2.绝对定位和浮动的区别和运用 3.CSS自适应宽度滑动门菜单   1)带下拉子菜单的导航菜单 带下拉子菜单的就是在一级导航下 ...

  8. springboot+cxf 开发webservice

    参考 https://www.cnblogs.com/fuxin41/p/6289162.html pom.xml <?xml version="1.0" encoding= ...

  9. Our Journey of Xian Ends

    Our Journey of Xian Ends https://nanti.jisuanke.com/t/18521 262144K   Life is a journey, and the roa ...

  10. 【校招面试 之 C/C++】第4题 拷贝构造函数被调用的3个时机

    1.被调用的3个时机: (1)直接初始化或拷贝初始化: (2)将一个对象作为一个实参传递,形参采用非指针或非引用的对象进行接收时(指针即指向了同一块空间,并未实现拷贝:而引用就是实参本身): (3)函 ...