第一題
照題目敘述去寫
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
第三題
我感覺是貪婪 但我想不到怎處理
總不可能直接模擬吧
放棄