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