題目出處
2273. Find Resultant Array After Removing Anagrams
難度
easy
個人範例程式碼
class Solution:
def removeAnagrams(self, words: List[str]) -> List[str]:
if not words:
return words
i = 0
last_word = ""
while(i < len(words)):
sort_word = "".join(sorted(words[i]))
if sort_word == last_word:
words.pop(i) # remove, and keep i
else:
last_word = sort_word
i += 1 # go next
return words
最近在練習程式碼本身就可以自解釋的 Coding style,可以嘗試直接閱讀程式碼理解
算法說明
讓我們先來理解題目,題目的意思簡單說就是,
我們將每個字都取 counter,如果前後 counter 相同,那我們就去把後面的刪除,
這邊要「特別注意」,題目只有說「前後」,因此「相隔很多的不用刪」,
input handling
如果沒有 input words,return words
Boundary conditions
for loop 控制範圍
特別注意,當 list remove 時,index 要繼續使用原來的 (因為原來位置被刪除,後面補前面)
Reference
⭐ Leetcode 解題紀錄 ⭐ | 題型 | 資料結構 | Python Solution | C++ Solution | Note | |
---|---|---|---|---|---|---|
⭐BFS 相關題型 ⭐ | ||||||
104 | Maximum Depth of Binary Tree | BFS (分層) | Python | |||
94 | Binary Tree Inorder Traversal | BFS (分層) | Tree | Python | 內含 處理 Tree 樹問題的重點 | |
102 | Binary Tree Level Order Traversal | BFS (分層) | Tree | Python | ||
103 | Binary Tree Zigzag Level Order Traversal | BFS (分層) | Tree | Python | ||
107 | Binary Tree Level Order Traversal II | BFS (分層) | Tree | Python | ||
133 | Clone Graph | BFS (分層) | Graph | Python | Graph 的基本操作 #重要題型 | |
127 | Word Ladder | BFS (分層), DFS | Graph | Python | ||
[Lint] 127 | Topological Sorting | BFS (拓撲) | Python | 內有 indegree, outdegree 介紹 #重要題型 | ||
207 | Course Schedule | BFS (拓樸) | Graph | Python | ||
210 | Course Schedule II | BFS (拓樸) | Graph | Python | ||
[Lint] 892 | Alien Dictionary | BFS (拓樸) | Graph | Python | ||
[Lint] 431 | Connected Component in Undirected Graph | BFS (連通塊) | Graph | Python | 內含 BFS 模板 #重要題型 | |
1091 | Shortest Path in Binary Matrix | BFS (最短路徑) | Matrix | Python | ||
⭐ Binary Serach 相關 |