lintcode(81)数据流中位数
来源:互联网 发布:java线程安全集合类 编辑:程序博客网 时间:2024/06/07 09:33
Description:
数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数。
中位数的定义:
- 中位数是排序后数组的中间值,如果有数组中有n个数,则中位数为A[(n-1)/2]。
- 比如:数组A=[1,2,3]的中位数是2,数组A=[1,19]的中位数是1。
Explanation:
持续进入数组的数的列表为:[1, 2, 3, 4, 5],则返回[1, 1, 2, 2, 3]
持续进入数组的数的列表为:[4, 5, 1, 3, 2, 6, 0],则返回 [4, 4, 4, 3, 3, 3, 3]
持续进入数组的数的列表为:[2, 20, 100],则返回[2, 2, 20]
Solution:
Set up arraylist record to dynamically add integer element, and then sort the record. The (i/2)th integer in the arraylist record is the current median.
public class Solution { /** * @param nums: A list of integers. * @return: the median of numbers */ public int[] medianII(int[] nums) { // write your code here int len = nums.length; int[] result = new int[len]; ArrayList<Integer> record = new ArrayList<Integer>(); for(int i = 0;i<len;i++){ record.add(nums[i]); Collections.sort(record); int count = i/2; result[i] = record.get(count); } return result; }}
阅读全文
0 0
- LintCode 81 -- 数据流中位数
- lintcode(81)数据流中位数
- 数据流中位数-LintCode
- LintCode 81. 数据流中位数
- LINTCODE——数据流中位数
- 数据流中位数
- LintCode-中位数
- LintCode:中位数
- 【LintCode】中位数
- LintCode-中位数
- LintCode 中位数
- 中位数-LintCode
- Lintcode中位数
- 数据流中的中位数
- 数据流中的中位数
- 求数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 人工智能基础复习5——学习
- ARP协议(地址解析协议)
- Coderforces Bit++
- Python的学习指导
- Mac React Native环境搭建,一路下来踩过的坑
- lintcode(81)数据流中位数
- Java虚拟机初步解析
- 单片机C语言实现数码管控制
- android--获取系统时间的几种方式
- Xamarin.Android 自定义 View
- Ice基本概念及术语
- 遍历-算法的万能钥匙
- 在CFile中读取里面的内容
- 数据结构--向量