-
- 陣列 (Array) / 字串 (String)
- 字串操作 (String Operation)
-
- Merge Strings Alternately (Easy)
-
- Greatest Common Divisor of Strings (Easy)
-
- Reverse Vowels of a String (Easy)
-
- Reverse Words in a String (Medium)
-
- String Compression (Medium)
-
- 陣列操作 (Array Operation)
-
- Kids With the Greatest Number of Candies (Easy)
-
- Can Place Flowers (Easy)
-
- Increasing Triplet Subsequence (Medium)
-
-
- 雙指針 (Two Pointers)
- 同向雙指針 (Same Direction)
-
- Move Zeroes (Easy)
-
- Is Subsequence (Easy)
-
- 相向雙指針 (Opposite Direction)
-
- Container With Most Water (Medium)
-
- Max Number of K-Sum Pairs (Medium)
-
-
- 滑動視窗 (Sliding Window)
- 固定視窗 (Fixed Window)
-
- Maximum Average Subarray I (Easy)
-
- Maximum Number of Vowels in a Substring of Given Length (Medium)
-
- 可變視窗 (Variable Window)
-
- Max Consecutive Ones III (Medium)
-
- Longest Subarray of 1's After Deleting One Element (Medium)
-
-
- 前綴和 (Prefix Sum)
- 前綴和應用 (Prefix Sum Application)
-
- Find the Highest Altitude (Easy)
-
- Find Pivot Index (Easy)
-
- Product of Array Except Self (Medium)
-
-
- 雜湊表 (Hash Map / Set)
- 集合運算 (Set Operations)
-
- Find the Difference of Two Arrays (Easy)
-
- Determine if Two Strings Are Close (Medium)
-
- 計數統計 (Frequency Count)
-
- Unique Number of Occurrences (Easy)
-
- Equal Row and Column Pairs (Medium)
-
-
- 堆疊 (Stack)
- 字串操作 (String Operation)
-
- Removing Stars From a String (Medium)
-
- Decode String (Medium)
-
- 模擬 (Simulation)
-
- Asteroid Collision (Medium)
-
- 6.1. 單調堆疊 (Monotonic Stack)
- 序列問題 (Sequence Problem)
-
- Daily Temperatures (Medium)
-
- Online Stock Span (Medium)
-
- 序列問題 (Sequence Problem)
-
- 佇列 (Queue)
- 佇列應用 (Queue Application)
-
- Number of Recent Calls (Easy)
-
- Dota2 Senate (Medium)
-
-
- 鏈結串列 (Linked List)
- 節點操作 (Node Operation)
-
- Reverse Linked List (Easy)
-
- Delete the Middle Node of a Linked List (Medium)
-
- Odd Even Linked List (Medium)
-
- 雙指針應用 (Two Pointers Application)
-
- Maximum Twin Sum of a Linked List (Medium)
-
-
- 二元樹 (Binary Tree)
- 9.1. 深度優先搜尋 (DFS)
- 樹的屬性 (Tree Properties)
-
- Maximum Depth of Binary Tree (Easy)
-
- Leaf-Similar Trees (Easy)
-
- Count Good Nodes in Binary Tree (Medium)
-
- Longest ZigZag Path in a Binary Tree (Medium)
-
- 路徑問題 (Path Problem)
-
- Path Sum III (Medium)
-
- Lowest Common Ancestor of a Binary Tree (Medium)
-
- 樹的屬性 (Tree Properties)
- 9.2. 廣度優先搜尋 (BFS)
- 層序遍歷 (Level Order Traversal)
-
- Binary Tree Right Side View (Medium)
-
- Maximum Level Sum of a Binary Tree (Medium)
-
- 層序遍歷 (Level Order Traversal)
-
- 二元搜尋樹 (Binary Search Tree)
- 搜尋與修改 (Search and Modify)
-
- Search in a Binary Search Tree (Easy)
-
- Delete Node in a BST (Medium)
-
-
- 堆積 (Heap / Priority Queue)
- 第 K 大/小元素 (Kth Largest/Smallest)
-
- Kth Largest Element in an Array (Medium)
-
- Smallest Number in Infinite Set (Medium)
-
- 最優化選擇 (Optimization)
-
- Maximum Subsequence Score (Medium)
-
- Total Cost to Hire K Workers (Medium)
-
-
- 字典樹 (Trie)
- 設計實作 (Design & Implementation)
-
- Implement Trie (Prefix Tree) (Medium)
-
- 搜尋應用 (Search Application)
-
- Search Suggestions System (Medium)
-
-
- 圖 (Graph)
- 13.1. 深度優先搜尋 (DFS)
- 連通性問題 (Connectivity)
-
- Keys and Rooms (Medium)
-
- Number of Provinces (Medium)
-
- 路徑問題 (Path Problem)
-
- Reorder Routes to Make All Paths Lead to the City Zero (Medium)
-
- Evaluate Division (Medium)
-
- 連通性問題 (Connectivity)
- 13.2. 廣度優先搜尋 (BFS)
- 最短路徑 (Shortest Path)
-
- Nearest Exit from Entrance in Maze (Medium)
-
- Rotting Oranges (Medium)
-
- 最短路徑 (Shortest Path)
-
- 二元搜尋 (Binary Search)
- 標準搜尋 (Standard Search)
-
- Guess Number Higher or Lower (Easy)
-
- 搜尋答案範圍 (Search on Answer)
-
- Successful Pairs of Spells and Potions (Medium)
-
- Find Peak Element (Medium)
-
- Koko Eating Bananas (Medium)
-
-
- 回溯 (Backtracking)
- 組合問題 (Combination)
-
- Letter Combinations of a Phone Number (Medium)
-
- Combination Sum III (Medium)
-
-
- 動態規劃 (Dynamic Programming)
- 16.1. 一維 DP (1D DP)
- 線性遞推 (Linear Recurrence)
-
- N-th Tribonacci Number (Easy)
-
- Min Cost Climbing Stairs (Easy)
-
- House Robber (Medium)
-
- Domino and Tromino Tiling (Medium)
-
- 線性遞推 (Linear Recurrence)
- 16.2. 多維 DP (Multidimensional DP)
- 路徑問題 (Path Problem)
-
- Unique Paths (Medium)
-
- 字串問題 (String Problem)
-
- Longest Common Subsequence (Medium)
-
- Edit Distance (Medium)
-
- 股票問題 (Stock Problem)
-
- Best Time to Buy and Sell Stock with Transaction Fee (Medium)
-
- 路徑問題 (Path Problem)
-
- 位元操作 (Bit Manipulation)
- 位元運算 (Bitwise Operation)
-
- Counting Bits (Easy)
-
- Single Number (Easy)
-
- Minimum Flips to Make a OR b Equal to c (Medium)
-
-
- 區間 (Intervals)
- 貪婪區間 (Greedy Interval)
-
- Non-overlapping Intervals (Medium)
-
- Minimum Number of Arrows to Burst Balloons (Medium)
-
初始化陣列,使用內建的 list 來建立動態陣列:
arr: list[int] = [1, 2, 3, 4, 5]
filled_arr: list[int] = [0] * 5
print(filled_arr) # [0, 0, 0, 0, 0]
s = "Hello"
lst = list(s)
print(lst) # ['H', 'e', 'l', 'l', 'o']訪問元素:
arr: list[int] = [1, 2, 3, 4, 5]
print(arr) # [1, 2, 3, 4, 5]
print(arr[0]) # 1
print(arr[-1]) # 5
print(arr[1:4]) # [2, 3, 4] ← 切片 (Slicing)
s = "Hello"
print(s[0]) # H
print(s[-1]) # o
print(s[1:4]) # ell ← 切片 (Slicing)插入元素:
arr: list[int] = [1, 2, 3, 4, 5]
arr.append(6)
print(arr) # [1, 2, 3, 4, 5, 6]
arr: list[int] = [1, 2, 3, 4, 5]
arr.insert(0, 99)
print(arr) # [99, 1, 2, 3, 4, 5]
arr: list[int] = [1, 2, 3, 4, 5]
arr.extend([6, 7, 8, 9, 10])
print(arr) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr: list[int] = [1, 2, 3, 4, 5]
new_arr = arr + [6, 7, 8, 9, 10]
print(new_arr) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]刪除元素:
arr: list[int] = [1, 2, 3, 4, 5]
arr.remove(3)
print(arr) # [1, 2, 4, 5]
arr: list[int] = [1, 2, 3, 4, 5]
val = arr.pop() # 移除最後一個
print(f"val = {val}, arr = {arr}") # val = 5, arr = [1, 2, 3, 4]
arr: list[int] = [1, 2, 3, 4, 5]
val = arr.pop(1) # 移除 index 1
print(f"val = {val}, arr = {arr}") # val = 2, arr = [1, 3, 4, 5]
arr: list[int] = [1, 2, 3, 4, 5]
del arr[0] # 刪除 index 0
print(arr) # [2, 3, 4, 5]
arr: list[int] = [1, 2, 3, 4, 5]
del arr[1:3] # 刪除 index 1~2
print(arr) # [1, 4, 5]
arr: list[int] = [1, 2, 3, 4, 5]
arr.clear()
print(arr) # []走訪陣列:
arr: list[int] = [10, 20, 30, 40, 50]
for idx, val in enumerate(arr):
print(f"index = {idx}, value = {val}")
# index = 0, value = 10
# index = 1, value = 20
# index = 2, value = 30
# index = 3, value = 40
# index = 4, value = 50雙指標 (Two Pointers) 走訪:
arr = [1, 2, 3, 4, 5]
left, right = 0, len(arr) - 1
while left <= right:
print(f"left={arr[left]}, right={arr[right]}")
left += 1
right -= 1
# left = 1, right = 5
# left = 2, right = 4
# left = 3, right = 3多陣列同步走訪:
names = ["Alice", "Bob", "Carol"]
scores = [60, 70, 90]
for name, score in zip(names, scores):
print(f"{name}:{score} 分")
# Alice:60 分
# Bob:70 分
# Carol:90 分給你兩個字串 word1 和 word2。請你從 word1 開始,透過交替加上字母來合併字串。如果一個字串比另一個字串長,就將多出來的字母追加到合併後字串的結尾。
回傳合併後的字串。
範例 1:
輸入: word1 = "abc", word2 = "pqr"
輸出: "apbqcr"
說明: 字串合併情況如下所示:
word1: a b c
word2: p q r
合併後: a p b q c r範例 2:
輸入: word1 = "ab", word2 = "pqrs"
輸出: "apbqrs"
說明: 注意,word2 比 word1 長,"rs" 需要追加到合併後字串的結尾。
word1: a b
word2: p q r s
合併後: a p b q r s範例 3:
輸入: word1 = "abcd", word2 = "pq"
輸出: "apbqcd"
說明: 注意,word1 比 word2 長,"cd" 需要追加到合併後字串的結尾。
word1: a b c d
word2: p q
合併後: a p b q c dfrom itertools import zip_longest
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
return "".join(a + b for a, b in zip_longest(word1, word2, fillvalue=""))
# ========== 測試 ==========
sol = Solution()
print(sol.mergeAlternately("abc", "pqr")) # "apbqcr"
print(sol.mergeAlternately("ab", "pqrs")) # "apbqrs"
print(sol.mergeAlternately("abcd", "pq")) # "apbqcd"使用雙指標 (Two Pointers):
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
i, j = 0, 0
result: list[str] = []
# 雙指標:兩字串都未耗盡時,交替取字元
while i < len(word1) and j < len(word2):
result.append(word1[i]) # 先取 word1 的字元
result.append(word2[j]) # 再取 word2 的字元
i += 1
j += 1
# 附加剩餘部分(最多只有一個字串有剩餘)
result.append(word1[i:]) # 若 word1 較長,附加其餘部分
result.append(word2[j:]) # 若 word2 較長,附加其餘部分
return "".join(result)
# ========== 測試 ==========
sol = Solution()
print(sol.mergeAlternately("abc", "pqr")) # "apbqcr"
print(sol.mergeAlternately("ab", "pqrs")) # "apbqrs"
print(sol.mergeAlternately("abcd", "pq")) # "apbqcd"