項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 search-input 尚未註冊或是沒有一個 view.php 檔案.

【Leetcode】python – [2273] Find Resultant Array After Removing Anagrams 個人解法筆記 | 293rd LeetCode Weekly Contest

題目出處

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 SolutionC++ SolutionNote
⭐BFS 相關題型 ⭐
104Maximum Depth of Binary TreeBFS (分層)Python
94Binary Tree Inorder TraversalBFS (分層)TreePython 內含 處理 Tree 樹問題的重點
102Binary Tree Level Order TraversalBFS (分層)TreePython
103Binary Tree Zigzag Level Order TraversalBFS (分層)TreePython
107Binary Tree Level Order Traversal IIBFS (分層)TreePython
133Clone GraphBFS (分層)GraphPython Graph 的基本操作 #重要題型
127Word LadderBFS (分層), DFSGraphPython
[Lint] 127Topological SortingBFS (拓撲)Python
內有 indegree, outdegree 介紹 #重要題型
207Course ScheduleBFS (拓樸)GraphPython
210Course Schedule IIBFS (拓樸)GraphPython
[Lint] 892Alien DictionaryBFS (拓樸)GraphPython
[Lint] 431Connected Component in Undirected GraphBFS (連通塊)GraphPython 內含 BFS 模板 #重要題型
1091Shortest Path in Binary MatrixBFS (最短路徑)MatrixPython
⭐ Binary Serach 相關