Re: [闲聊] 每日leetcode

楼主: dont   2024-09-10 11:19:29
2807. Insert Greatest Common Divisors in Linked List
## 思路
照着做 中间塞gcd
## Code
```python
class Solution:
def insertGreatestCommonDivisors(self, head: Optional[ListNode]) ->
Optional[ListNode]:
def get_gcd(x, y):
while y:
x, y = y, x % y
return x
curr = head
while curr and curr.next:
gcd = get_gcd(curr.val, curr.next.val)
curr.next = node = ListNode(gcd, curr.next)
curr = node.next
return head
```
817. Linked List Components
## 思路
先把nums转成set再扫LinkedList
如果node在nums里就res+1并且跳过相连且在nums里的node
## Code
```python
class Solution:
def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:
nums = set(nums)
res = 0
while head:
if head.val in nums:
res += 1
while head and head.val in nums:
head = head.next
else:
head = head.next
return res
```
作者: sustainer123 (caster)   2024-09-10 11:21:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com