Re: [閒聊] 每日leetcode

看板Marginalman作者 (JerryChung)時間1年前 (2024/11/24 02:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1149/1548 (看更多)
※ 引述《Rushia (早瀬ユウカの体操服 )》之銘言: : https://leetcode.com/problems/rotating-the-box/description/ : 1861. Rotating the Box : 給你一個字元陣列表示一個箱子 : # 表示石頭 : * 表示地板 : . 表示空氣 : 求出我們把箱子往右翻轉90度之後石頭的分布狀況,如果石頭下面是空氣會往下掉。 : 思路: 石頭就塞到 temp 空氣就塞到 answer 地板就把 temp 的石頭塞到 answer 再加上 石頭 清空 temp 做完就是答案了 可以先每行開一個 result 最後再 map zip 或是直接邊判斷邊塞進 answer 就放成績最好的解吧 不過其實都差不多就是了 Python Code: class Solution: def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]: answer = [] # 或直接 [[] for _ in range(len(box[0]))] for b in range(-1, -len(box)-1, -1): # 轉90度 底部變在左邊 n = 0 temp = [] for i in box[b]: if b == -1: # 上面用[]的話 在循環一開始塞[] answer.append([]) # 否則省略 if i == '.': answer[n].append(i) n += 1 elif i == '#': temp.append(i) else: for t in temp: # 塞石頭 answer[n].append(t) n += 1 answer[n].append(i) # 加入地板 n += 1 temp = [] else: for t in temp: # 循環完temp還有東西的話要記得塞 answer[n].append(t) n += 1 return answer 話說11月漏4天了 重新來過囉 好耶 呵呵 嗚嗚 哇哇哇 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.55.250 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1732386765.A.014.html
文章代碼(AID): #1dGX_D0K (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dGX_D0K (Marginalman)