牛客 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)];
}
相关推荐
多米Domi0117 分钟前
0x3f 第25天 黑马web (145-167)hot100链表
数据结构·python·算法·leetcode·链表
LYFlied8 分钟前
【每日算法】LeetCode 207. 课程表
算法·leetcode·职场和发展
sali-tec9 分钟前
C# 基于OpenCv的视觉工作流-章7-膨胀
图像处理·人工智能·opencv·算法·计算机视觉
叫我:松哥13 分钟前
基于机器学习的地震风险评估与可视化系统,采用Flask后端与Bootstrap前端,系统集成DBSCAN空间聚类算法与随机森林算法
前端·算法·机器学习·flask·bootstrap·echarts·聚类
一起养小猫14 分钟前
LeetCode100天Day12-删除重复项与删除重复项II
java·数据结构·算法·leetcode
小O的算法实验室20 分钟前
2023年IEEE TITS SCI2区TOP,增强遗传算法+分布式随机多无人机协同区域搜索路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
Allen_LVyingbo28 分钟前
病历生成与质控编码的工程化范式研究:从模型驱动到系统治理的范式转变
前端·javascript·算法·前端框架·知识图谱·健康医疗·easyui
一起努力啊~28 分钟前
算法刷题--螺旋矩阵II+区间和+开发商购买土地
数据结构·算法·leetcode
Swift社区28 分钟前
LeetCode 470 用 Rand7() 实现 Rand10()
算法·leetcode·职场和发展
闻缺陷则喜何志丹31 分钟前
【图论 DFS 换根法】3772. 子图的最大得分|2235
c++·算法·深度优先·力扣·图论·换根法