牛客 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)];
}
相关推荐
凌肖战1 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
88号技师2 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
ゞ 正在缓冲99%…2 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss3 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
知己如祭3 小时前
图论基础(DFS、BFS、拓扑排序)
算法
mit6.8243 小时前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug3 小时前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒3 小时前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
军训猫猫头4 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success4 小时前
【爆刷力扣-数组】二分查找 及 衍生题型
算法