File tree Expand file tree Collapse file tree 3 files changed +77
-2
lines changed
solution/0000-0099/0004.Median of Two Sorted Arrays Expand file tree Collapse file tree 3 files changed +77
-2
lines changed Original file line number Diff line number Diff line change 7676<!-- 这里可写当前语言的特殊实现逻辑 -->
7777
7878``` python
79-
79+ class Solution :
80+ def findMedianSortedArrays (self , nums1 : List[int ], nums2 : List[int ]) -> float :
81+ # concatenate the 2 lists and sort them
82+ nums1 += nums2
83+ nums1.sort()
84+ length = len (nums1)
85+ value = length/ 2
86+ if length % 2 == 0 :
87+ value = int (value)
88+ return (nums1[value- 1 ] + nums1[value])/ 2
89+ else :
90+ return nums1[int (value)]
8091```
8192
8293### ** Java**
8798
8899```
89100
101+ ### ** Nim**
102+
103+ ``` nim
104+ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float =
105+ var
106+ fullList: seq[int] = concat(nums1, nums2)
107+ value: int = fullList.len div 2
108+
109+ fullList.sort()
110+
111+ if fullList.len mod 2 == 0:
112+ result = (fullList[value - 1] + fullList[value]) / 2
113+ else:
114+ result = fullList[value].toFloat()
115+ ```
116+
90117### ** ...**
91118
92119```
Original file line number Diff line number Diff line change 6666### ** Python3**
6767
6868``` python
69-
69+ class Solution :
70+ def findMedianSortedArrays (self , nums1 : List[int ], nums2 : List[int ]) -> float :
71+ # concatenate the 2 lists and sort them
72+ nums1 += nums2
73+ nums1.sort()
74+ length = len (nums1)
75+ value = length/ 2
76+ if length % 2 == 0 :
77+ value = int (value)
78+ return (nums1[value- 1 ] + nums1[value])/ 2
79+ else :
80+ return nums1[int (value)]
7081```
7182
7283### ** Java**
7586
7687```
7788
89+ ### ** Nim**
90+
91+ ``` nim
92+ proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float =
93+ var
94+ fullList: seq[int] = concat(nums1, nums2)
95+ value: int = fullList.len div 2
96+
97+ fullList.sort()
98+
99+ if fullList.len mod 2 == 0:
100+ result = (fullList[value - 1] + fullList[value]) / 2
101+ else:
102+ result = fullList[value].toFloat()
103+ ```
104+
78105### ** ...**
79106
80107```
Original file line number Diff line number Diff line change 1+ import std/ [algorithm, sequtils]
2+
3+ proc medianOfTwoSortedArrays (nums1: seq [int ], nums2: seq [int ]): float =
4+ var
5+ fullList: seq [int ] = concat (nums1, nums2)
6+ value: int = fullList.len div 2
7+
8+ fullList.sort ()
9+
10+ if fullList.len mod 2 == 0 :
11+ result = (fullList[value - 1 ] + fullList[value]) / 2
12+ else :
13+ result = fullList[value].toFloat ()
14+
15+ # Driver Code
16+
17+ # var
18+ # arrA: seq[int] = @[1, 2]
19+ # arrB: seq[int] = @[3, 4, 5]
20+ # echo medianOfTwoSortedArrays(arrA, arrB)
21+
You can’t perform that action at this time.
0 commit comments