zoukankan      html  css  js  c++  java
  • leetcode——4.寻找两个有序数组的中位数

    class Solution:
        def findMedianSortedArrays(self, nums1, nums2) -> float:
            c=sorted(nums1+nums2)
            if len(c)%2==0:
                return (c[len(c)//2]+c[len(c)//2-1])/2
            else:
                return float(c[(len(c)-1)//2])
    执行用时 :104 ms, 在所有 Python3 提交中击败了85.76%的用户
    内存消耗 :14.1 MB, 在所有 Python3 提交中击败了5.28%的用户
                                                                                                     ——2019.10.7
     

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
            //递归
            int n = nums1.length;
            int m = nums2.length;
            int left = (n+m+1) / 2;
            int right = (n+m+2) /2;
            return (getKth(nums1,0,n-1,nums2,0,m-1,left) + getKth(nums1,0,n-1,nums2,0,m-1,right)) * 0.5;
        }
    
        private double getKth(int[] nums1, int start1, int end1, int[] nums2, int start2, int end2, int k) {
            int len1 = end1 - start1 + 1;
            int len2 = end2 - start2 + 1;
            if(len1 > len2){    //让len1的长度小于len2,这样如果有数组空了,就能保证一定是len1
                return getKth(nums2,start2,end2,nums1,start1,end1 ,k);
            }
            if(len1 == 0){
                return nums2[start2 + k - 1];
            }
            if(k == 1){
                return Math.min(nums1[start1],nums2[start2]);
            }
            int i = start1 + Math.min(len1,k/2)-1;
            int j = start2 + Math.min(len2,k/2)-1;
            if(nums1[i] > nums2[j]){
                return getKth(nums1,start1,end1,nums2,j+1,end2,k-(j - start2 +1));
            }else{
                return getKth(nums1,i+1,end1,nums2,start2,end2,k-(i - start1 +1));
            }
        }

    ——2020.7.9


    public double findMedianSortedArrays(int[] nums1, int[] nums2) {   //递归做法下午再做
            int n1 = nums1.length;
            int n2 = nums2.length;
            int len = n1+n2;
            if(len%2==1){
                int medIndex = len/2;
                return getKthElement(nums1,nums2,medIndex+1,0,0,n1,n2);
            }else{
                int medIndex1 = len/2-1,medIndex2 = len/2;
                double median1 = getKthElement(nums1,nums2,medIndex1+1,0,0,n1,n2);
                double median2 = getKthElement(nums1,nums2,medIndex2+1,0,0,n1,n2);
                return (median1+median2)/2.0;
            }
        }
    
        private int getKthElement(int[] nums1, int[] nums2, int k,int index1,int index2,int n1,int n2) {
            if(index1 == n1){
                return nums2[index2+k-1];
            }
            if(index2 == n2){
                return nums1[index1+k-1];
            }
            if(k == 1){
                return Math.min(nums1[index1],nums2[index2]);
            }
    
            int half = k/2;
            int newIndex1 = Math.min(index1+half,n1)-1;
            int newIndex2 = Math.min(index2+half,n2)-1;
            if(nums1[newIndex1]<=nums2[newIndex2]){
                k -= (newIndex1 - index1 +1);
                index1 = newIndex1 + 1;
                return getKthElement(nums1,nums2,k,index1,index2,n1,n2);
            }else{
                k -= (newIndex2 - index2 +1);
                index2 = newIndex2 + 1;
                return getKthElement(nums1,nums2,k,index1,index2,n1,n2);
            }
        }

    递归。

        public double findMedianSortedArrays(int[] nums1, int[] nums2) {   //递归做法下午再做
            int n1 = nums1.length;
            int n2 = nums2.length;
            int len = n1+n2;
            if(len%2==1){
                int medIndex = len/2;
                double median = getKthElement(nums1,nums2,medIndex+1);
                return median;
            }else{
                int medIndex1 = len/2-1,medIndex2 = len/2;
                double median1 = getKthElement(nums1,nums2,medIndex1+1);
                double median2 = getKthElement(nums1,nums2,medIndex2+1);
                double median = (median1+median2)/2.0;
                return median;
            }
        }
    
        private int getKthElement(int[] nums1, int[] nums2, int k) {
            int n1 = nums1.length;
            int n2 = nums2.length;
            int index1 = 0,index2 = 0;
            while (true){
                if(index1 == n1){
                    return nums2[index2+k-1];
                }
                if(index2 == n2){
                    return nums1[index1+k-1];
                }
                if(k == 1){
                    return Math.min(nums1[index1],nums2[index2]);
                }
    
                int half = k/2;
                int newIndex1 = Math.min(index1+half,n1)-1;
                int newIndex2 = Math.min(index2+half,n2)-1;
                if(nums1[newIndex1]<=nums2[newIndex2]){
                    k -= (newIndex1 - index1 +1);
                    index1 = newIndex1 + 1;
                }else{
                    k -= (newIndex2 - index2 +1);
                    index2 = newIndex2 + 1;
                }
            }
        }

    ——2020.8.31

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    C语言程序设计第5堂作业
    C语言程序设计第4堂作业
    gets和fgets函数的区别
    编写一个程序实现strcpy函数的功能
    编写一个程序实现strcmp函数的功能
    编写一个程序实现strcat函数的功能
    11-1. 通讯录的录入与显示(10)
    11-0. 平面向量加法(10)
    10-4. 字符串循环左移(20)
    10-3. 字符串逆序(15)
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11630179.html
Copyright © 2011-2022 走看看