avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2 .Two numbers

  2024-01-01        
字数统计: 388字   |   阅读时长: 2min

topic:

2023-07-03.png
[2].Two numbers.md

Thought:

I originally wrote the stupidest way,把两个Linked转换成数字,Then add,再转换成Linked。But it’s strange that I was right when I was running locally,But it’s wrong when you submit,
What is deducted is a very strange oneprecompiled.listnode.ListNode,But mine is'__main__.ListNode'。
So I can only watch0x3fs answer。

Two node values ​​each time`l1.val`,`l2.val`In -digit`carry`Add,Divide 10 The remaining number is the digital digit that the current node needs to be saved,Divide10The business is the new position value

Code implementation,There is a trick to simplify code:ifrecursion中发现l2Length ratio ratiol1Longer,So it can be exchangedl1and l2,ensure l1Not an empty node,So as to simplify code logic。

Code:

1
2
3
4
5
6
7
8
9
10
11
class Solution:
# l1 and l2 Node for current traversal,carry Be in place
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode], carry=0) -> Optional[ListNode]:
if l1 is None and l2 is None: # recursion边界:l1 and l2 All empty nodes
return ListNode(carry) if carry else None # If you are in place,Just create an additional node
if l1 is None: # if l1 Empty,Then then l2 一定Not an empty node
l1, l2 = l2, l1 # exchange l1 and l2,ensure l1 non empty,从而简化Code
carry += l1.val + (l2.val if l2 else 0) # 节点值andcarry加在一起
l1.val = carry % 10 # Each node saves a digital position
l1.next = self.addTwoNumbers(l1.next, l2.next if l2 else None, carry // 10) # carry
return l1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode], carry = 0) -> Optional[ListNode]:
l1_list = []
l2_list=[]
def recursion_list(node:ListNode, listt):
if not node:
return listt
listt.append(node.val)
return recursion_list(node.next, listt)
l1_list = recursion_list(l1, l1_list)
l2_list = recursion_list(l2, l2_list)
l1_list = ''.join(map(str, l1_list))
l2_list = ''.join(map(str, l2_list))
l3 = str(int(l1_list)+int(l2_list))
print(l3)
def recursion_linkArray(num):
if not num:
return None
head = ListNode(0)
current = head
for c in num:
current.next = ListNode(int(c))
current = current.next
return head.next
head = recursion_linkArray(l3[::-1])
print(recursion_list(head, []))
print(type(l1), type(head))
return head
  • Python
  • answer
  • math
  • Linked
  • recursion

扫一扫,分享到微信

微信分享二维码
2596.Check the Cavaliers patrol plan
630.Class ScheduleIII
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号