兩個排序陣列的中位數

2021-08-29 00:04:46 字數 994 閱讀 6587

來自leetcode的兩個排序陣列的中位數

給定兩個大小為 m 和 n 的有序陣列 nums1 和 nums2 。

請找出這兩個有序陣列的中位數。//要求演算法的時間複雜度為 o(log (m+n)) 。

你可以假設 nums1 和 nums2 不同時為空。

思路:合併陣列

對陣列排序

得到中位數

**片.

// findmediansortedarrays

public

static double findmediansortedarrays

(int[

] nums1, int[

] nums2)

int[

] a3 =

newint

[nums1.length + nums2.length]

; system.

arraycopy

(nums1,

0, a3,

0, nums1.length)

; system.

arraycopy

(nums2,

0, a3, nums1.length, nums2.length)

;

a3就是合併後的陣列;

**片.

// 這裡使用嘟嘟嘟排序對a3排序

int temp =-1

;for

(int i =

0; i < a3.length; i++)}

}

**片.

// 很樸素吧

double x;if(

(a3.length)%2

==0)else

x即中位數;

print hello world.

兩個排序陣列的中位數

求兩個排序陣列中位數,這道題是很有意思的一道題目,演算法導論中9.3 8題,這題必須在o logn 的時間複雜度求解,否則肯定悲劇。這題有個關鍵的條件,那就是這兩個陣列長度相等 思路如下 陣列a 1,3,5,7,9 陣列b 2,4,6,8,10 首先取二者的中位數,在o 1 時間複雜度內求出,那麼陣...

兩個排序陣列的中位數

求兩個排序陣列中位數,這道題是很有意思的一道題目,演算法導論中9.3 8題,這題必須在o logn 的時間複雜度求解,否則肯定悲劇。這題有個關鍵的條件,那就是這兩個陣列長度相等 思路如下 陣列a 1,3,5,7,9 陣列b 2,4,6,8,10 首先取二者的中位數,在o 1 時間複雜度內求出,那麼陣...

兩個排序陣列的中位數

兩個排序的陣列a和b分別含有m和n個數,找到兩個排序陣列的中位數,要求時間複雜度應為o log m n 給出陣列a 1,2,3,4,5,6 b 2,3,4,5 中位數3.5 給出陣列a 1,2,3 b 4,5 中位數 3 public double findmediansortedarrays in...