From bd73b753b66f96a652e131820d4beb58678fa78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Tue, 4 Jul 2023 10:30:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=9F=A9=E9=98=B5=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../problem_solving_02_01.py" | 43 +++++++++++++++++++ .../problem_solving_02_02.py" | 35 +++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 "04-\347\237\251\351\230\265/problem_solving_02_01.py" create mode 100644 "04-\347\237\251\351\230\265/problem_solving_02_02.py" diff --git "a/04-\347\237\251\351\230\265/problem_solving_02_01.py" "b/04-\347\237\251\351\230\265/problem_solving_02_01.py" new file mode 100644 index 0000000..e1d64f6 --- /dev/null +++ "b/04-\347\237\251\351\230\265/problem_solving_02_01.py" @@ -0,0 +1,43 @@ +""" +矩阵中的和 +""" +from typing import List + + +class Solution: + def matrixSum(self, nums: List[List[int]]) -> int: + """ + 行转列 先遍历矩阵,保存index-max的关系 + :param nums: + :return: + """ + for row in nums: + row.sort() + res = 0 + dict1 = {} + for i in range(len(nums)): + for j in range(len(nums[0])): + if j in dict1: + dict1[j] = max(dict1[j], nums[i][j]) + else: + dict1[j] = nums[i][j] + for key in dict1: + res += dict1[key] + return res + + +if __name__ == '__main__': + result = Solution().matrixSum([[1, 8, 16, 15, 12, 9, 15, 11, 18, 6, 16, 4, 9, 4], + [3, 19, 8, 17, 19, 4, 9, 3, 2, 10, 15, 17, 3, 11], + [13, 10, 19, 20, 6, 17, 15, 14, 16, 8, 1, 17, 0, 2], + [12, 20, 0, 19, 15, 10, 7, 10, 2, 6, 18, 7, 7, 4], + [17, 14, 2, 2, 10, 16, 15, 3, 9, 17, 9, 3, 17, 10], + [17, 6, 19, 17, 18, 9, 14, 2, 19, 12, 10, 18, 7, 9], + [5, 6, 5, 1, 19, 8, 15, 2, 2, 4, 4, 1, 2, 17], + [12, 16, 8, 16, 7, 6, 18, 13, 18, 8, 14, 15, 20, 11], + [2, 10, 19, 3, 15, 18, 20, 10, 6, 7, 0, 8, 3, 7], + [11, 5, 10, 13, 1, 3, 4, 7, 1, 18, 20, 17, 19, 2], + [0, 3, 20, 6, 19, 18, 3, 12, 2, 11, 3, 1, 19, 0], + [6, 5, 3, 15, 6, 1, 0, 17, 13, 19, 3, 8, 2, 7], + [2, 20, 9, 11, 13, 5, 1, 16, 14, 1, 19, 3, 12, 6]]) + print(result) diff --git "a/04-\347\237\251\351\230\265/problem_solving_02_02.py" "b/04-\347\237\251\351\230\265/problem_solving_02_02.py" new file mode 100644 index 0000000..0b2f2e7 --- /dev/null +++ "b/04-\347\237\251\351\230\265/problem_solving_02_02.py" @@ -0,0 +1,35 @@ +""" +矩阵中的和 +""" +from typing import List +import numpy as np + + +class Solution: + def matrixSum(self, nums: List[List[int]]) -> int: + """ + axis=0表示列 + axis=1表示行 + :param nums: + :return: + """ + for i in nums: + i[:] = sorted(i) + return int(sum(np.max(np.array(nums), axis=0))) + + +if __name__ == '__main__': + result = Solution().matrixSum([[1, 8, 16, 15, 12, 9, 15, 11, 18, 6, 16, 4, 9, 4], + [3, 19, 8, 17, 19, 4, 9, 3, 2, 10, 15, 17, 3, 11], + [13, 10, 19, 20, 6, 17, 15, 14, 16, 8, 1, 17, 0, 2], + [12, 20, 0, 19, 15, 10, 7, 10, 2, 6, 18, 7, 7, 4], + [17, 14, 2, 2, 10, 16, 15, 3, 9, 17, 9, 3, 17, 10], + [17, 6, 19, 17, 18, 9, 14, 2, 19, 12, 10, 18, 7, 9], + [5, 6, 5, 1, 19, 8, 15, 2, 2, 4, 4, 1, 2, 17], + [12, 16, 8, 16, 7, 6, 18, 13, 18, 8, 14, 15, 20, 11], + [2, 10, 19, 3, 15, 18, 20, 10, 6, 7, 0, 8, 3, 7], + [11, 5, 10, 13, 1, 3, 4, 7, 1, 18, 20, 17, 19, 2], + [0, 3, 20, 6, 19, 18, 3, 12, 2, 11, 3, 1, 19, 0], + [6, 5, 3, 15, 6, 1, 0, 17, 13, 19, 3, 8, 2, 7], + [2, 20, 9, 11, 13, 5, 1, 16, 14, 1, 19, 3, 12, 6]]) + print(result) -- GitLab