Re: [闲聊] Leetcode Weekly Contest 406

楼主: sustainer123 (caster)   2024-07-14 11:40:23
第一题
照题目叙述去写
Python Code:
class Solution:
def getSmallestString(self, s: str) -> str:
s = list(s)
for i in range(1,len(s)):
if int(s[i]) < int(s[i-1]) and (int(s[i]) % 2) == (int(s[i-1]) %
2):
s[i-1] , s[i] = s[i], s[i-1]
break
return "".join(s)
第二题:
照题目叙述去写
Python Code:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def modifiedList(self, nums: List[int], head: Optional[ListNode]) ->
Optional[ListNode]:
nums = set(nums)
tmp = ListNode()
tmp.next = head
pre = tmp
cur = head
while cur:
if cur.val in nums:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return tmp.next
第三题
我感觉是贪婪 但我想不到怎处理
总不可能直接模拟吧
放弃
作者: SecondRun (雨夜琴声)   2024-07-14 11:41:00
泥板胜我没刷题了
楼主: sustainer123 (caster)   2024-07-14 11:42:00
宝 你以后开公司收刷题仔
作者: Rushia (みけねこ的鼻屎)   2024-07-14 11:43:00
第三题是回溯法吧
楼主: sustainer123 (caster)   2024-07-14 11:44:00
对欸 20而已

Links booklink

Contact Us: admin [ a t ] ucptt.com