牛客 NC36 在两个长度相等的排序数组中找到上中位数【中等 模拟 Java,Go,PHP】

题目

题目链接:

https://www.nowcoder.com/practice/6fbe70f3a51d44fa9395cfc49694404f

思路

复制代码
	直接模拟2个数组有顺序放到一个数组中,然后返回中间的数

参考答案java

java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
         *
         * find median in two sorted array
         * @param arr1 int整型一维数组 the array1
         * @param arr2 int整型一维数组 the array2
         * @return int整型
         */
        public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) {
            int n = arr1.length;
            int m= arr2.length;
            int len = n+m;
            int[] help = new int[len];
            int x = n-1;
            int y=m-1;
            int z = len-1;
            while (x>=0 && y>=0) {
                if(arr1[x] >arr2[y]){
                    help[z--] = arr1[x--];
                }else{
                    help[z--] = arr2[y--];
                }
            }

            while (x>=0){
                help[z--] = arr1[x--];
            }

            while (y>=0){
                help[z--] = arr2[y--];
            }


          return help[(len-1)/2];
        }
    }

参考答案Go

go 复制代码
package main



/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * find median in two sorted array
 * @param arr1 int整型一维数组 the array1
 * @param arr2 int整型一维数组 the array2
 * @return int整型
 */
func findMedianinTwoSortedAray(arr1 []int, arr2 []int) int {
	n := len(arr1)
	m := len(arr2)

	length := n + m

	help := make([]int, length)
	x := n - 1
	y := m - 1
	z := length - 1
	for x >= 0 && y >= 0 {
		if arr1[x] >= arr2[y] {
			help[z] = arr1[x]
			z--
			x--
		} else {
			help[z] = arr2[y]
			z--
			y--
		}
	}

	for x >= 0 {
		help[z] = arr1[x]
		z--
		x--
	}

	for y >= 0 {
		help[z] = arr2[y]
		z--
		y--
	}

	return help[(length-1)/2]
}

参考答案PHP

php 复制代码
<?php


/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * find median in two sorted array
 * @param arr1 int整型一维数组 the array1
 * @param arr2 int整型一维数组 the array2
 * @return int整型
 */
function findMedianinTwoSortedAray( $arr1 ,  $arr2 )
{
    $n = count($arr1);
    $m= count($arr2);
    $len = $n+$m;
    $help = array();

    $x = $n-1;
    $y = $m-1;
    $z = $len-1;
    while ($x >=0 && $y>=0){
        if($arr1[$x] >$arr2[$y]){
            $help[$z--] = $arr1[$x--];
        }else{
            $help[$z--] = $arr2[$y--];
        }
    }

    while ($x>=0){
        $help[$z--]= $arr1[$x--];
    }

    while ($y>=0){
        $help[$z--] = $arr2[$y--];
    }

    return $help[intval(($len-1)/2)];
}
相关推荐
总斯霖几秒前
HDU 4857 - 逃生 题解
算法
-To be number.wan1 分钟前
算法学习日记 | 模拟
c++·学习·算法
Blossom.1182 分钟前
从“金鱼记忆“到“超级大脑“:2025年AI智能体记忆机制与MoE架构的融合革命
人工智能·python·算法·架构·自动化·whisper·哈希算法
金枪不摆鳍2 分钟前
算法-贪心算法
算法·贪心算法
naruto_lnq2 分钟前
高性能消息队列实现
开发语言·c++·算法
池央4 分钟前
贪心算法-摆动序列
算法·贪心算法
AndrewHZ5 分钟前
【AI黑话日日新】什么是隐式CoT?
人工智能·深度学习·算法·llm·cot·复杂推理
格林威9 分钟前
Baumer相机视野内微小缺陷增强检测:提升亚像素级瑕疵可见性的 7 个核心方法,附 OpenCV+Halcon 实战代码!
人工智能·数码相机·opencv·算法·计算机视觉·视觉检测·工业相机
进击的荆棘1 小时前
优选算法——滑动窗口
c++·算法·leetcode
csdn_aspnet1 小时前
奈飞工厂算法:个性化推荐系统的极限复刻
算法·netflix·奈飞