临时文本

/* 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);	
    	}
    }
}
上一篇:两个数组的交集 II --java


下一篇:0子序列中等 LeetCode718. 最长重复子数组