Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
004.median_of_two_sorted_arrays (Hard)
links:
Title: https://oj.leetcode.com/problems/Median-of-Two-Sorted-Arrays/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
Find the median of two ordered arrays. The requirement complexity is O (log (n + m)).
Analysis:
Two ways of thinking:
1. Merge two arrays directly and then seek the median, but the complexity is O (n + m).
2. Use the idea of two points to do, this is not good to think, but also to consider the parity. Can transform thinking, to find two ordinal array of the K-large number, so it is better to think about.
Code:
1. Merge
C++:
Class Solution {public:double findmediansortedarrays (int a[], int m, int b[], int n) {vector<int> c;int pa = 0, Pb = 0;//Point of A & Bwhile (PA < m | | PB < N) {if (Pa = = m) {c.push_back (b[pb++]); continue;} if (Pb = = N) {c.push_back (a[pa++]); continue;} if (A[pa] > B[PB]) c.push_back (b[pb++]); Elsec.push_back (a[pa++]);} if ((n + m) &1) return c[(n+m)/2];elsereturn (c[(n+m)/2-1] + c[(n+m)/2])/2.0;};
2. Two points
C++:
Class Solution {private:double findkthsortedarrays (int a[], int m, int b[], int n, int k) {if (M < n) {swap (n, m); Swap ( A, B);} if (n = = 0) return a[k-1];if (k = = 1) return min (a[0], b[0]), int pb = min (K/2, n), PA = k-pb;if (A[pa-1] > B[PB- 1]) return Findkthsortedarrays (A, M, B + PB, N-PB, K-PB), else if (A[pa-1] < b[pb-1]) return Findkthsortedarrays ( A + PA, m-pa, B, N, k-pa); Elsereturn a[pa-1];} public:double findmediansortedarrays (int a[], int m, int b[], int n) {if ((n + m) &1) return Findkthsortedarrays (A, M, B , N, (n + m)/2 + 1); Elsereturn (Findkthsortedarrays (A, m, B, N, (n + m)/2 + 1) +findkthsortedarrays (A, m, B, N, (n + M) )/2))/2.0;};
Java:
public class Solution {private double findkthsortedarrays (int a[], int astart, int aend, int b[], int bstart, int bend, int k) {int m = aend-astart, n = bend-bstart; if (M < n) {return findkthsortedarrays (B, bstart, bend, A, Astart, Aend, K); } if (n = = 0) return A[astart + k-1]; if (k = = 1) return math.min (A[astart], B[bstart]); int PB = Math.min (K/2, n), PA = K-PB; if (A[astart + pa-1] > B[bstart + pb-1]) return findkthsortedarrays (A, Astart, Aend, B, bstart + PB, Ben D, K-PB); else if (A[astart + Pa-1] < B[bstart + pb-1]) return findkthsortedarrays (A, Astart + PA, aend, B, bstart , Bend, K-PA); else return A[astart + pa-1]; } public double findmediansortedarrays (int a[], int b[]) {int m = a.length, n = b.length; if ((n + m)% 2 = = 1) return findkthsorTedarrays (A, 0, M, B, 0, N, (n + m)/2 + 1); else return (Findkthsortedarrays (a, 0, M, B, 0, N, (n + m)/2 + 1) + findkthsortedarrays (A, 0, M, B, 0, N, (n + m)/2))/2.0; }}
Python:
class Solution:def findkthsortedarrays (self, A, B, K): If Len (a) < Len (b): tmp = a A = B b = tmp If Len (B) = = 0:return A[k-1] if k = = 1:return min (a[0], b[0]) PB = Min (K/2, Len (B)) PA = K-PB if a[pa-1] > b[pb-1]: Return Self.findkthsortedarra Ys (A, B[PB:], K-PB) elif A[pa-1] < B[pb-1]: return Self.findkthsortedarrays (A[PA:], B, K-PA) Else:return A[pa-1] # @return A float def findmediansortedarrays (self, A, B): if (Len (a) + len (b))% 2 = = 1:return Self.findkthsortedarrays (A, B, (Len (a) + len (B))/2 + 1) else:r Eturn (Self.findkthsortedarrays (A, B, Len (a) + len (B))/2) + Self.findkthsortedarrays (A, B, Len (a) + len (B))/2 + 1)/2.0
[Leetcode] 004. Median of Sorted Arrays (Hard) (C++/java/python)