80. 中位数(简单题)

给定一个未排序的整数数组,找到其中位数。

中位数是排序后数组的中间值,如果数组的个数是偶数个,则返回排序后数组的第N/2个数。

样例

给出数组[4, 5, 1, 2, 3], 返回 3

给出数组[7, 9, 4, 5],返回 5

挑战

时间复杂度为O(n)

  • 工具:python3
  • 分析:先进行排序,再进行查找中间的元素

代码:

class Solution:
"""
@param nums: A list of integers
@return: An integer denotes the middle number of the array
"""
def median(self, nums):
# write your code here
nums.sort()
length_nums = len(nums)
i = int(length_nums/2)-1
j = int((length_nums+1)/2)-1
if length_nums % 2 ==0:
return nums[i]
else:
return nums[j]

lintcode-80.中位数的更多相关文章

  1. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  2. [LintCode笔记了解一下]80.Median

    Given a unsorted array with integers, find the median of it. A median is the middle number of the ar ...

  3. [LintCode] 两个排序数组的中位数

    class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  8. Vijos P1459 车展 treap求任意区间中位数

    描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...

  9. vijos P1459 车展(Treap,中位数)

    P1459车展   描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的 ...

随机推荐

  1. win10系统驱动备份及还原

    win10系统驱动备份及还原 方法如下: 1.右键单击开始按钮,选择“命令提示符(管理员)” 2.输入如下命令后按回车,等待备份完成: dism /online /export-driver /des ...

  2. react native错误排查-TypeError: window.deltaUrlToBlobUrl is not a function

    错误现象:window.deltaUrlToBlobUrl is not a function 最近在调试react-native时,打开浏览器调试时发现报错window.deltaUrlToBlob ...

  3. 超详细的纯净windows系统重装示例

    之前说过通过使用大白菜制作启动盘,再去系统之家下载系统.虽然系统能安装,但是有时安装的是ghost版本,安装方便,只要引导后面就是等待自动安装,驱动什么的都不用管.但是有时会带上很多软件,不好清理甚至 ...

  4. Oracle 11g新特性direct path read引发的系统停运故障诊断处理

    黎俊杰 | 2016-07-28 14:37 声明:部分表名为了脱敏而用XX代替 1.故障现象 (1)一个业务系统输入用户名与密码后无法进入首页,表现为一直在运行等待,运行缓慢 (2)整个系统无法正常 ...

  5. 五、Linux_ping命令

    ping命令用法为:“ping 参数 目标主机”.其中参数为零到多个,目标主机可以是IP或者域名. 1.每隔0.6秒ping一次,一共ping 5次: [root@aiezu.com ~]# ping ...

  6. 【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】

    思路 题意:题目为中文题,这里不再过多阐述. 思路1:可以在读入单词表的过程中将单词分解,用map将它一 一记录 思路2:利用字典树,这个方法较快些,下面代码中会分别给出数组和结构体指针两种形式的字典 ...

  7. 性能测试基础---URL和HTTP协议

    ·URL和HTTP协议: ·URL构成: URL是web应用进行资源访问的主要方式.一般来说,由五个部分构成: 示例:http://192.168.2.212/phpwind1/searcher.ph ...

  8. Maven之setting.xml 配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  9. Python使用pip安装matplotlib模块

    matplotlib是python中强大的画图模块. 首先确保已经安装python,然后用pip来安装matplotlib模块. 进入到cmd窗口下,建议执行python -m pip install ...

  10. 《Java周边》vue开发环境搭建(windows)

    1. NodeJs 安装包下载 百度云:链接: https://pan.baidu.com/s/169TdKRLZd0dXbKSGTr8evw 提取码: th4a 复制这段内容后打开百度网盘手机App ...