From 9711c0b31dae2b9fdf85d3e5ade7381770c1ec34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 2 Jul 2023 11:30:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E9=93=BE=E8=A1=A8=20=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../problem_solving_01.py" | 51 +++++++++++++++++++ .../problem_solving_01.py" | 29 +++++++++-- 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 "14_\345\210\267\351\242\230/02-\351\223\276\350\241\250/problem_solving_01.py" diff --git "a/14_\345\210\267\351\242\230/02-\351\223\276\350\241\250/problem_solving_01.py" "b/14_\345\210\267\351\242\230/02-\351\223\276\350\241\250/problem_solving_01.py" new file mode 100644 index 0000000..5f428ae --- /dev/null +++ "b/14_\345\210\267\351\242\230/02-\351\223\276\350\241\250/problem_solving_01.py" @@ -0,0 +1,51 @@ +""" +两数相加 +""" +from typing import List, Optional + + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: + """ + 链表遍历 + :param l1: + :param l2: + :return: + """ + res = ListNode(0) + cur = res + carry = 0 + while l1 or l2: + x = l1.val if l1 else 0 + y = l2.val if l2 else 0 + s = x + y + carry + carry = s // 10 + cur.next = ListNode(s % 10) + cur = cur.next + if l1: + l1 = l1.next + if l2: + l2 = l2.next + if carry: + cur.next = ListNode(carry) + return res.next + + +if __name__ == '__main__': + l1 = ListNode(2) + l1.next = ListNode(4) + l1.next.next = ListNode(3) + + l2 = ListNode(5) + l2.next = ListNode(6) + l2.next.next = ListNode(4) + result = Solution().addTwoNumbers(l1, l2) + while result: + print(result.val, end=' -> ') + result = result.next diff --git "a/14_\345\210\267\351\242\230/04-\347\237\251\351\230\265/problem_solving_01.py" "b/14_\345\210\267\351\242\230/04-\347\237\251\351\230\265/problem_solving_01.py" index 48287fc..54be576 100644 --- "a/14_\345\210\267\351\242\230/04-\347\237\251\351\230\265/problem_solving_01.py" +++ "b/14_\345\210\267\351\242\230/04-\347\237\251\351\230\265/problem_solving_01.py" @@ -6,10 +6,33 @@ from typing import List class Solution: def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]: - - pass + # 初始化结果集 + n = len(colsum) + res = [[0] * n for _ in range(2)] + for i in range(n): + if colsum[i] == 2: + res[0][i] = 1 + res[1][i] = 1 + upper -= 1 + lower -= 1 + elif colsum[i] == 0: + res[0][i] = 0 + res[1][i] = 0 + else: + if upper > lower: + res[0][i] = 1 + res[1][i] = 0 + upper -= 1 + else: + res[0][i] = 0 + res[1][i] = 1 + lower -= 1 + if upper != 0 or lower != 0: + return [] + return res if __name__ == '__main__': - result = Solution().reconstructMatrix(2, 1, [1, 1, 1]) + # result = Solution().reconstructMatrix(2, 1, [1, 1, 1]) + result = Solution().reconstructMatrix(5, 5, [2, 1, 2, 0, 1, 0, 1, 2, 0, 1]) print(result) -- GitLab