From bb0838d7993748147caba7f0977ce17e5cce8132 Mon Sep 17 00:00:00 2001 From: uwivbn Date: Sat, 24 Aug 2019 16:53:01 +0800 Subject: [PATCH 1/8] leetcode 4 --- .idea/vcs.xml | 6 +++ 4. Median of Two Sorted Arrays/code.py | 58 +++++++++++++++++++++++-- 4. Median of Two Sorted Arrays/code1.py | 22 ++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 4. Median of Two Sorted Arrays/code1.py diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/4. Median of Two Sorted Arrays/code.py b/4. Median of Two Sorted Arrays/code.py index 4b92eab..b79b030 100644 --- a/4. Median of Two Sorted Arrays/code.py +++ b/4. Median of Two Sorted Arrays/code.py @@ -1,23 +1,73 @@ from typing import List +import math class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: m_index = [] + m_val = [] l_1, l_2 = len(nums1), len(nums2) - aver_len = l_1 + l_2 // 2 - if aver_len == 0: - m_index.extend([aver_len-1, aver_len]) + aver_len = (l_1 + l_2) // 2 + if (l_1 + l_2) % 2 == 0: + m_index.extend([aver_len-2, aver_len-1]) else: m_index.append(aver_len) + # print("中位数在整体数组中的索引位置:", m_index) + p_l1, p_l2, cur = 0, 0, 0 + while p_l1 < l_1 and p_l2 < l_2: + if nums1[p_l1] < nums2[p_l2]: + p_l1 += 1 + else: + p_l2 += 1 + cur += 1 + if cur > int(math.fsum(m_index)): + break + # print('cur:', cur, '\np_l1:', p_l1, '\np_l2:', p_l2) + if cur <= int(math.fsum(m_index)): # 说明两个数组,至少有一个数组已经扫描结束 + if p_l2 == l_2: + p_l2 = p_l2 - 1 + temp = nums2[p_l2] + arr = nums1 + p = int(math.fsum(m_index)) - p_l2 + else: + p_l1 = p_l1 - 1 + temp = nums1[p_l1] + arr = nums2 + p = int(math.fsum(m_index)) - p_l1 + + flag, ind = 0, 0 + print(m_index) + print(p) + for i in range(len(m_index)): + if (flag == 0) and min(arr[p + ind], temp) == temp: + flag = 1 + m_val.append(temp) + else: + m_val.append(arr[p + ind]) + ind += 1 + else: + # 说明中位数在两个指针前部,两个数组的指针都没有扫描结束 + if p_l1 == l_1: + p_l1 = l_1 - 1 + else: + p_l2 = l_2 - 1 + m_val.append(min(nums1[p_l1], nums2[p_l2])) + if len(m_index) > 1: + m_val.append(max(nums1[p_l1], nums2[p_l2])) + + return math.fsum(m_val) / len(m_val) if __name__ == '__main__': solution = Solution() - nums1 = [1, 2] + nums1 = [1, 2, 7, 9] nums2 = [3, 4] + # nums1 = [1, 3] + # nums2 = [2] + # nums1 = [] + # nums2 = [1] print((solution.findMedianSortedArrays(nums1, nums2))) diff --git a/4. Median of Two Sorted Arrays/code1.py b/4. Median of Two Sorted Arrays/code1.py new file mode 100644 index 0000000..409e6fb --- /dev/null +++ b/4. Median of Two Sorted Arrays/code1.py @@ -0,0 +1,22 @@ +from typing import List +import math + + +class Solution: + + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + + return + + +if __name__ == '__main__': + solution = Solution() + nums1 = [1, 2, 7, 9] + nums2 = [3, 4] + # nums1 = [1, 3] + # nums2 = [2] + # nums1 = [] + # nums2 = [1] + + print((solution.findMedianSortedArrays(nums1, nums2))) + From be4336a8e03b1bff6817309913a57dd3f2cf2ad5 Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:05:20 +0800 Subject: [PATCH 2/8] Create .gitignore_global --- ~/.gitignore_global | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ~/.gitignore_global diff --git a/~/.gitignore_global b/~/.gitignore_global new file mode 100644 index 0000000..7b92988 --- /dev/null +++ b/~/.gitignore_global @@ -0,0 +1,21 @@ +# Compiled source # +################### +*.idea + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db + From 9e8c9036d2ad6cf28b552ba4907266f095ffe0dc Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:06:51 +0800 Subject: [PATCH 3/8] Delete .gitignore_global --- ~/.gitignore_global | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 ~/.gitignore_global diff --git a/~/.gitignore_global b/~/.gitignore_global deleted file mode 100644 index 7b92988..0000000 --- a/~/.gitignore_global +++ /dev/null @@ -1,21 +0,0 @@ -# Compiled source # -################### -*.idea - -# Logs and databases # -###################### -*.log -*.sql -*.sqlite - -# OS generated files # -###################### -.DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -Icon? -ehthumbs.db -Thumbs.db - From dd6ad163481b91197619281206d62613bb8f502c Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:08:12 +0800 Subject: [PATCH 4/8] Create .gitignore_global --- .gitignore_global | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .gitignore_global diff --git a/.gitignore_global b/.gitignore_global new file mode 100644 index 0000000..0ab24cd --- /dev/null +++ b/.gitignore_global @@ -0,0 +1,20 @@ +# Compiled source # +*.idea + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db + From 35fc623e849a1b511e5706e418bf383ebae5efad Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:09:46 +0800 Subject: [PATCH 5/8] Delete .gitignore_global --- .gitignore_global | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 .gitignore_global diff --git a/.gitignore_global b/.gitignore_global deleted file mode 100644 index 0ab24cd..0000000 --- a/.gitignore_global +++ /dev/null @@ -1,20 +0,0 @@ -# Compiled source # -*.idea - -# Logs and databases # -###################### -*.log -*.sql -*.sqlite - -# OS generated files # -###################### -.DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -Icon? -ehthumbs.db -Thumbs.db - From 3ed1ec9d15a81d3c77e34c5cf0bdd7534ac6f186 Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:11:03 +0800 Subject: [PATCH 6/8] Create .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffd2a4a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.idea From 9037fe80406444b2ce16169b073552a121f08cde Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:13:26 +0800 Subject: [PATCH 7/8] Delete .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ffd2a4a..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.idea From c861d8271ce0e30a58cf7f92ff9d1df187e5a0ff Mon Sep 17 00:00:00 2001 From: ChunyuPCY <18896534787@163.com> Date: Sat, 24 Aug 2019 20:14:24 +0800 Subject: [PATCH 8/8] Delete vcs.xml --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file