일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- 715. Range Module
- iterator
- LeetCode
- 30. Substring with Concatenation of All Words
- 프로그래머스
- Class
- Python
- data science
- Substring with Concatenation of All Words
- Decorator
- 컴퓨터의 구조
- Convert Sorted List to Binary Search Tree
- t1
- 파이썬
- Regular Expression
- kaggle
- 43. Multiply Strings
- Python Code
- attribute
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- DWG
- 밴픽
- shiba
- 315. Count of Smaller Numbers After Self
- Protocol
- 운영체제
- 시바견
- Generator
- concurrency
Archives
- Today
- Total
Scribbling
LeetCode: 2. Add Two Numbers 본문
1. 노가다성 답안
class Solution:
def addTwoNumbers(self, l1, l2, up=0):
if l1 != None and l2 != None:
val1 = l1.val
val2 = l2.val
val = (val1 + val2 + up) % 10
up = (val1 + val2 + up) // 10
ret_node = ListNode(val=val)
ret_node.next = self.addTwoNumbers(l1.next, l2.next, up=up)
elif l1 == None and l2 != None:
val2 = l2.val
val = (val2 + up) % 10
up = (val2 + up) // 10
ret_node = ListNode(val=val)
ret_node.next = self.addTwoNumbers(l1, l2.next, up=up)
elif l1 != None and l2 == None:
val1 = l1.val
val = (val1 + up) % 10
up = (val1 + up) // 10
ret_node = ListNode(val=val)
ret_node.next = self.addTwoNumbers(l1.next, l2, up=up)
else:
val = up
if up != 0:
ret_node = ListNode(val=val)
else:
return None
return ret_node
2. 짧은 답안
class Solution:
def addTwoNumbers(self, l1, l2, up=0):
val = (l1.val + l2.val + up) % 10
up = (l1.val + l2.val + up) // 10
ret_node = ListNode(val=val)
now_node = ret_node
while l1.next != None or l2.next != None or up != 0:
if l1.next == None:
l1.next = ListNode(val=0)
if l2.next == None:
l2.next = ListNode(val=0)
val = (l1.next.val + l2.next.val + up) % 10
up = (l1.next.val + l2.next.val + up) // 10
next_node = ListNode(val=val)
now_node.next = next_node
now_node = next_node
l1 = l1.next
l2 = l2.next
return ret_node
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 5. Longest Palindromic Substring (0) | 2021.08.12 |
---|---|
LeetCode: 787. Cheapest Flights Within K Stops (0) | 2021.08.12 |
LeetCode: 3. Longest Substring Without Repeating Characters (0) | 2021.08.11 |
LeetCode: 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance (0) | 2021.08.10 |
LeetCode: 743. Network Delay Time (0) | 2021.08.10 |