牛客 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)];
}
相关推荐
Han.miracle3 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
mit6.8245 小时前
前后缀分解
算法
你好,我叫C小白6 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
寂静山林8 小时前
UVa 10228 A Star not a Tree?
算法
Neverfadeaway8 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
Madison-No79 小时前
【C++】探秘vector的底层实现
java·c++·算法
Swift社区9 小时前
LeetCode 401 - 二进制手表
算法·leetcode·ssh
派大星爱吃猫9 小时前
顺序表算法题(LeetCode)
算法·leetcode·职场和发展
liu****9 小时前
8.list的模拟实现
linux·数据结构·c++·算法·list
地平线开发者10 小时前
征程 6 | 征程 6 工具链如何支持 Matmul/Conv 双 int16 输入量化?
算法·自动驾驶