LeetCode题目----求中位数---标签:Array
题目难度---困难
题目要求:
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。
请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。
思路:第一眼看到题目两个数组求中位数,看似很复杂,但是仔细一想,两个数组合在一块不久行了?然后合并后的数组给他排序,进而判断是奇数位数组还是偶数位数组
ok!代码奉上:
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] result = new int[nums1.length + nums2.length];// 首先初始化合并数组的大小
List list = new LinkedList();// 建立新数组,目的是list集合插入方便
if (result.length == 0) {// 判断数组是否为空
return 0;
}
// 将两个数组中的值放入list集合中
for (int i : nums1) {
list.add(i);
}
for (int j : nums2) {
list.add(j);
}
// 这部很关键--因为后面遍历整个数组排序数组会因为这部操作而大大简化
for (int i = 0; i < list.size(); i++) {
result[i] = (Integer) list.get(i);// 很简单的将list集合的元素一个个遍历到result数组中
}
// 下面就是新数组及合并后的数组排序
for (int i = 0; i < result.length; i++) {
for (int j = i + 1; j < result.length; j++) {
if (result[i] >= result[j]) {
int temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
// -----------------这个地方就是判断数组的元素是奇数偶数,奇数取最中间的数就可以了,偶数就中间俩位取平均了
double answer = 0;
if (result.length % 2 == 1) {
int q = result.length / 2;
answer = result[q];
} else {
int p = result.length / 2;
for (int i = 0; i < result.length; i++) {
if (p - 1 == i) {
answer = result[i];
break;
}
}
answer = (answer + result[p]) / 2;
}
// 上边注意的地方:数组元素计算奇数偶数后取值时应注意遍历是从0开始,因此result.length/2的值要比遍历变量大1
return answer;
}

看似很完美是吧!但是!!!时间复杂度的要求O(log (m+n)) 。
因为题目难度属于“困难”,因此不会这么简单,在参考了好多人的答案后,慢慢理解了。
思路:
LeetCode题目----求中位数---标签:Array的更多相关文章
- leetcode题目4.寻找两个有序数组的中位数(困难)
题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 ...
- LeetCode 697. Degree of an Array (数组的度)
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- POJ 2388 Who's in the Middle(水~奇数个数排序求中位数)
题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #in ...
- LeetCode Two Sum II - Input array is sorted
原题链接在这里:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 题目: Given an array of intege ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- [Leetcode 90]求含有重复数的子集 Subset II
[题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- LeetCode K-diff Pairs in an Array
原题链接在这里:https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description 题目: Given an array of i ...
随机推荐
- Leetcode 5——Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- MyGod--Beta版本前期报告
下一阶段需要改进完善的功能 1.完善购买功能,商品购买后,将生成申请订单,卖家将收到提醒.卖家在完成订单后,可以选择完成订单,商品将下架. 2.完善搜索功能,将界面中的搜索功能添加进去(简单考虑只搜索 ...
- 学号:201621123032 《Java程序设计》第9周学习总结(
1:本周学习总结 1.1:以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容 2:书面作业 2.1: List中指定元素的删除(题集题目) 2.1.1:实验总结.并回答:列举至少2种在List ...
- ajax实现无刷新分页效果
基于jquery.pagination.js实现的无刷新加载分页数据效果. 简介与说明 * 该插件为Ajax分页插件,一次性加载数据,故分页切换时无刷新与延迟.如果数据量较大,加载会比较慢. * 分页 ...
- Session 和 Cookie 区别
会话跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.==Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用 ...
- JAVA_SE基础——60.初识Object
java是面向对象的语言,核心思想:找适合 的对象做适合 的事情:方式一:自定义类,然后通过自定义的类创建对象.方式二:sun提供了很多的类给我使用,我们只需要认识这些类,我们就可以通过这些类创建对象 ...
- django启动uwsgi报错
查看uwsgi.log *** Starting uWSGI 2.0.17 (64bit) on [Thu Apr 5 17:46:15 2018] *** compiled with version ...
- 从PRISM开始学WPF(八)導航Navigation?
0x6Navigation Basic Navigation Prism中的Navigation提供了一种类似导航的功能,他可以根据用户的输入,来刷新UI. 先看一个最简单的例子,通过按钮来导航到一个 ...
- SpringBoot入门:Spring Data JPA 和 JPA(理论)
参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...
- SpringMvc采用 http+json 实现前后端交互
演示列表 报文表示 一.Json请求和Json响应 实现:Spring4.1.1.RELEASE + jackson2.4.4+JQuery1.10.2 1.pom.xml <propertie ...