临时文本

本文解析了如何使用C#和Java实现LeetCode题目中两个已排序数组的中位数计算。通过迭代合并数组元素,找到中位数位置并返回结果。重点讲解了如何在O(log(min(n1,n2)))时间内解决该问题。
摘要由CSDN通过智能技术生成

/* 4. Median of Two Sorted Arrays: https://leetcode.com/problems/median-of-two-sorted-arrays */https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1182548/c

public class Solution {
    public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
        var mediumPositionLeft = (nums1.Length+nums2.Length-1)/2;
        var mediumPositionRight = (nums1.Length+nums2.Length)/2;
        if (mediumPositionLeft<0)
            return 0;
        var currentPosition = 0;
        var index1 = 0;
        var index2 = 0;
        var result = 0.0;
        while (currentPosition<=mediumPositionRight)
        {
            var nextNumber = 0;
            if (index1>=nums1.Length)
            {
                nextNumber = nums2[index2];
                index2++;
            }
            else if (index2>=nums2.Length)
            {
                nextNumber = nums1[index1];
                index1++;
            }
            else if (nums1[index1]<nums2[index2])
            {
                nextNumber = nums1[index1];
                index1++;    
            }
            else
            {
                nextNumber = nums2[index2];
                index2++;
            }
            if (currentPosition==mediumPositionLeft)
                result+=nextNumber;
            if (currentPosition==mediumPositionRight)
                result+=nextNumber;
            
            currentPosition++;
            
        }
        return    result/2.0;    
        
    }
}
/* 4. Median of Two Sorted Arrays: https://leetcode.com/problems/median-of-two-sorted-arrays */
using System;
public class Solution {
    public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
        return 1;
    }
    public void Sort(int[] args){
    	int temp;
    	for(int i = 0; i < args.Length; i++){    		
    		temp = args[i];
    		for(int j = i+1 ; j < args.Length; j++){
    			if(args[j] < temp){
    				args[i] = args[j];
    				args[j] = temp;	
                    temp = args[i];
    			}	
    		}
    	}
    }
    public void CopyArgs(int[] args1, int[] args2){
        for(int i = args1.Length, j = 0; args2[j] !='\0'; i++, j++){
            args1[i] = args2[j];
        }
    }
    public int GetArrEffiLen(int[] args){
        int i = 0;
        while(args[i] == 0) {
            i++;
        }
        return i;
    }
    static void Main(){
    	Solution s = new Solution();
    	int [] arr1 = new int[5]  { 99,  98, 92, 97, 95};
    	int [] arr2 = new int[5]  { 9,  8, 2, 7, 5};
        int [] arr3  = new int[arr1.Length + arr2.Length];
        //s.CopyArgs(arr3,arr1);
        Console.WriteLine("数组3的有效长度:{0}", s.GetArrEffiLen(arr3));
    	foreach(int element in arr3){
    		Console.WriteLine("{0}",element);	
    	}
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MrFlySand_飞沙

公众号【小知识酷】,搜索获取更

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值