“415ba029eaf29131b75656a41235ce5aa265bff7”上不存在“PPOCRLabel/data/git@gitcode.net:s920243400/PaddleOCR.git”
提交 9711c0b3 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:链表 两数相加

上级 fc7588f3
"""
两数相加
"""
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
......@@ -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)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册