作者sustainer123 (caster )
看板Marginalman
標題Re: [閒聊] Leetcode Weekly Contest 406
時間Sun Jul 14 11:40:23 2024
第一題
照題目敘述去寫
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
第三題
我感覺是貪婪 但我想不到怎處理
總不可能直接模擬吧
放棄
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.123.233 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1720928425.A.B89.html
推 SecondRun: 泥板勝我沒刷題了 07/14 11:41
→ sustainer123: 寶 你以後開公司收刷題仔 07/14 11:42
→ Rushia: 第三題是回溯法吧 07/14 11:43
→ sustainer123: 對欸 20而已 07/14 11:44