lintcode-中位数-80
来源:互联网 发布:编程达人在线教育 编辑:程序博客网 时间:2024/04/29 08:28
给定一个未排序的整数数组,找到其中位数。
中位数是排序后数组的中间值,如果数组的个数是偶数个,则返回排序后数组的第N/2个数。
样例
给出数组[4, 5, 1, 2, 3], 返回 3
给出数组[7, 9, 4, 5],返回 5
挑战 时间复杂度为O(n)
空间换时间:
class Solution {public: int median(vector<int> &nums) { map<int,int> vis; int count; int minp=nums[0]; int maxp=nums[0]; for(auto e:nums){ ++vis[e]; minp=min(e,minp); maxp=max(e,maxp); } int n=nums.size(); count=(n&1)?n/2+1:n/2; for(int i=minp;i<=maxp;++i){ while(vis[i]>0){ --vis[i],--count; if(count==0) return i; } } }};
0 0
- lintcode-中位数-80
- lintcode python代码 80中位数
- LintCode-中位数
- LintCode:中位数
- 【LintCode】中位数
- LintCode-中位数
- LintCode 中位数
- 中位数-LintCode
- Lintcode中位数
- LintCode#80FindMedian(利用快排找中位数)
- lintcode&九章算术——Lintcode No.80 中位数 ? 待解决
- LintCode 81 -- 数据流中位数
- Lintcode——中位数
- lintcode(81)数据流中位数
- 数据流中位数-LintCode
- LintCode 81. 数据流中位数
- 滑动窗口的中位数-LintCode
- LINTCODE——数据流中位数
- 赛码在线编程1001
- Android手机屏幕到手机间的距离
- [BZOJ1637][Usaco2007 Mar]Balanced Lineup
- volatile and non-cacheable
- Windows环境下Android Studio下载安装
- lintcode-中位数-80
- iOS远程推送
- hdoj 1052Tian Ji -- The Horse Racing 【贪心】
- Andorid的面试题
- 揭开Socket编程的面纱
- 编写高性能的Lua代码
- Android从捕获全局异常说起
- HDU 1166 敌兵布阵 一维树状数组or线段树
- 技术人生故事1:从BASIC起步