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. vue插件(还真是第一次接触)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Typora-yes:typora最舒适的使用-优化主题+图床服务+自动上传图片插件

    转载注明出处:https://www.cnblogs.com/nreg/p/11992678.html,谢谢 开源项目下载:https://github.com/nreg/typora-yes 云盘: ...

  3. Clickhouse高可用配置总结

    1. 简述 Clickhouse默认是多分片单副本集群,分布式表的配置是每个分片只有一份,如果某个节点挂掉的话,则会直接导致写入或查询异常:Clickhouse是具有高可用特性的,即每个分片具有2个或 ...

  4. cnetos7安装mysql并开启慢日志查询

    参考博客地址https://www.cnblogs.com/luyucheng/p/6265594.html 安装部署(5.7) #下载Yum Repository curl -O https://r ...

  5. 《linux就该这么学》课堂笔记02 虚拟机安装使用

    这节学习了虚拟机安装RHEL系统,了解了shell.以及命令的格式            

  6. Kali下的内网劫持(三)

    前面两种说的是在Kali下的ettercap工具通过配合driftnet和urlsnarf进行数据捕获,接下来我要说的是利用Kali下的另外一种抓包分析工具——wireshark来进行捕获数据: 首先 ...

  7. Java中线程池,你真的会用吗?ExecutorService ThreadPoolExcutor

    原文:https://www.hollischuang.com/archives/2888 在<深入源码分析Java线程池的实现原理>这篇文章中,我们介绍过了Java中线程池的常见用法以及 ...

  8. django项目中使用手机号登录

    本文使用聚合数据的短信接口,需要先获取到申请接口的appkey和模板id 项目目录下创建ubtils文件夹,定义返回随机验证码和调取短信接口的函数 function.py文件 import rando ...

  9. 正式一点的my.cnf文件

    感觉比以前的文件,配置正式了一些. 留照. [client] port #socket=/mysql/mysql.sock default-character-set=utf8 [mysqld] po ...

  10. Python 推送RabbitMQ

    username = 'xxxxxxxx' pwd = 'xxxxxxxx' user_pwd = pika.PlainCredentials(username, pwd) s_conn = pika ...