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;
    }

emmmm第一篇自己写的博客,有问题请留言……^-^

原文地址:https://www.cnblogs.com/wangsr-suc/p/8848363.html

时间: 2024-10-31 11:56:15

LeetCode题目----求中位数---标签:Array的相关文章

Leetcode题目:Merge Sorted Array

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

leetcode题目思路以及部分解答(二)

又刷了30题了,这速度还不错.因为还有别的东西要复习,所以进度并不快.感觉还是能学到很多新东西的.早知道这个就不用去其他地方刷了.这个难度不高,还可以知道哪些情况没考虑.比其他OJ那种封闭式的好多了.还是进入正题吧. 1.Rotate Image 这个做过两三次了,但每次还是得重新开始推导..这次又推导了很久..不过好在做过,代码也写得比较简洁. 主要思路就是第一层循环按层次深入.第二层把旋转后对应替代的4个位置循环更新.swap就是用来更新用的.做完发现讨论里的最高票代码就是我这样子= =  

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

POJ 2388 Who&#39;s in the Middle(水~奇数个数排序求中位数)

题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 int na[n+1]; 10 for(int i=0; i

leetcode第1题(array)

题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note t

[LeetCode] Search in Rotated Sorted Array [35]

题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no dup

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

[LeetCode] Search in Rotated Sorted Array II [36]

题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted