lintcode-80.中位数
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.中位数的更多相关文章
- [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 ...
- [LintCode笔记了解一下]80.Median
Given a unsorted array with integers, find the median of it. A median is the middle number of the ar ...
- [LintCode] 两个排序数组的中位数
class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Vijos P1459 车展 treap求任意区间中位数
描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...
- vijos P1459 车展(Treap,中位数)
P1459车展 描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的 ...
随机推荐
- linux 安装Python3.6
1.安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel ...
- HTTP协议的认识
1.内容回顾 1.HTTP协议消息的格式: 1.请求(request) 请求方法 路径 HTTP/1.1 \r\n k1:v1\r\n ...\r\n \r\n 请求体 <--这里可以有可以没有 ...
- fulltext全文索引的使用
Fulltext全文索引 Fulltext相关属性 查看数据库关于fulltext的配置 SHOW VARIABLES LIKE 'ft%'; -- ft就是FullText的简写 ft_boolea ...
- jQuery实现点击图片简单放大效果
一.HTML代码如下: <img class="comment_pics" width="50px" height="50px" sr ...
- Oracle 逻辑存储结构
一.总述 逻辑存储结构是 Oracle 数据库存储结构的核心内容,对 Oracle 数据库的所有操作都会涉及逻辑存储结构.逻辑存储结构是从逻辑的角度分析数据库的组成,是对数据存储结构在逻辑概念上的划分 ...
- day 03 作业 预科
目录 作业 1.简述变量的组成 2.简述变量名的命名规范 3.简述注释的作用 4.使用turtle库构造一幅图,贴在markdown文档中 作业 1.简述变量的组成 变量由变量名.赋值符号.变量值所组 ...
- 如何在Win7电脑上增加新磁盘分区?
原文链接: https://www.cnblogs.com/haoxitong/p/9405497.html 我们在重装好系统Win7系统后有时会碰到需要新建磁盘分区的情况,这时我们再重装系统进行磁盘 ...
- linux备份mysql文件并恢复的脚本,以及其中出现的错误:ERROR: ASCII '\0' appeared in the statement
首先是在网上找了一下教程,代码很简单 #!/bin/bash folder=~/test time=`date +%Y%m%d` mysqldump -u user -p pwd -hlocalhos ...
- 25.centos7基础学习与积累-011-课前考试二-命令练习
从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 取IP地址: 6的命令:ifconfig eth0 7的命令 [root@pytho ...
- https://www.runoob.com/linux/mysql-install-setup.html
https://www.runoob.com/linux/mysql-install-setup.html