牛客题霸-算法篇
在两个长度相等的排序数组中找到上中位数
相似的企业真题
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 256M,其他语言512M
热度指数:7745
本题知识点:
数组
二分
分治
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
给定两个有序数组arr1和arr2,已知两个数组的长度都为N,求两个数组中所有数的上中位数。
上中位数:假设递增序列长度为n,若n为奇数,则上中位数为第n/2+1个数;否则为第n/2个数
[要求]
时间复杂度为
,额外空间复杂度为
示例1
输入
复制
[1,2,3,4],[3,4,5,6]
[1,2,3,4],[3,4,5,6]
返回值
复制
3
3
说明
总共有8个数,上中位数是第4小的数,所以返回3。
示例2
输入
复制
[0,1,2],[3,4,5]
[0,1,2],[3,4,5]
返回值
复制
2
2
说明
总共有6个数,那么上中位数是第3小的数,所以返回2
备注:
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
import java.util.*; 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) { // write code here } }
class Solution { public: /** * find median in two sorted array * @param arr1 int整型vector the array1 * @param arr2 int整型vector the array2 * @return int整型 */ int findMedianinTwoSortedAray(vector
& arr1, vector
& arr2) { // write code here } };
# # find median in two sorted array # @param arr1 int整型一维数组 the array1 # @param arr2 int整型一维数组 the array2 # @return int整型 # class Solution: def findMedianinTwoSortedAray(self , arr1 , arr2 ): # write code here
/** * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ function findMedianinTwoSortedAray( arr1 , arr2 ) { // write code here } module.exports = { findMedianinTwoSortedAray : findMedianinTwoSortedAray };
# # find median in two sorted array # @param arr1 int整型一维数组 the array1 # @param arr2 int整型一维数组 the array2 # @return int整型 # class Solution: def findMedianinTwoSortedAray(self , arr1 , arr2 ): # write code here
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 { // write code here }
/** * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr1Len int arr1数组长度 * @param arr2 int整型一维数组 the array2 * @param arr2Len int arr2数组长度 * @return int整型 */ int findMedianinTwoSortedAray(int* arr1, int arr1Len, int* arr2, int arr2Len ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # find median in two sorted array # @param arr1 int整型一维数组 the array1 # @param arr2 int整型一维数组 the array2 # @return int整型 # class Solution def findMedianinTwoSortedAray(arr1, arr2) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ def findMedianinTwoSortedAray(arr1: Array[Int],arr2: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ fun findMedianinTwoSortedAray(arr1: IntArray,arr2: IntArray): Int { // write code here } }
import java.util.*; 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) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ export function findMedianinTwoSortedAray(arr1: number[], arr2: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 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 { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ pub fn findMedianinTwoSortedAray(&self, arr1: Vec
, arr2: Vec
) -> i32 { // write code here } }
[1,2,3,4],[3,4,5,6]
3
[0,1,2],[3,4,5]
2