https://leetcode.com/problems/number-of-matching-subsequences/ Brute-force (Time Limit Exceeded) Time Complexity: O(len(s) * len(words)) 在檢查 s 的每一個字元時 都要遍歷 words 一次 浪費太多不必要的時間 class Solution: def numM ...
https://leetcode.com/problems/delete-nodes-and-return-forest/ Solution class Solution: def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]: to_delete = set(to_delet ...
https://leetcode.com/problems/find-in-mountain-array/ 相關例題 Leetcode # 852. Peak Index in a Mountain Array Solution 先找到山頂(參考 Leetcode # 852. Peak Index in a Mountain Array ) 從山頂將 array 一分為二 各別做 binary ...
https://leetcode.com/problems/peak-index-in-a-mountain-array/ Solution 如果只用三點來判斷是否為 mountain array 容易發生如上圖的誤判 所以使用斜率,來判斷中點位於上坡或下坡 Time Complexity: O(log(len(arr))) Space Complexity: O(1) class Solutio ...
https://leetcode.com/problems/01-matrix/ ⭐️ Solution: Breadth-First Serch Time Complexity: O(r * c) Space Complexity: O(1) ※ 不計算為了 output 所使用的 memory class Solution: def updateMatrix(self, mat: List[L ...
https://leetcode.com/problems/reverse-linked-list/ Solution Time Complexity: O(n) Space Complexity: O(1) Python 版 class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNod ...
https://leetcode.com/problems/single-number/ XOR \begin{align} 1. \, & p \oplus 0 = p \\ 2. \, & p \oplus p = 0 \\ 3. \, & 有交換律: p \oplus q = q \oplus p \\ & \Rightarrow p \oplus q \o ...
Bitwise(位元運算) a & b Bitwise AND a | b Bitwise OR a ^ b Bitwise XOR (exclusive OR) ~a Bitwise NOT a << n Bitwise left shift a >> n Bitwise right shift 相關例題 Leetcode # 190. Reverse Bits ...
iterator 有__next__()以取得 iterator 的下一個item 有__iter__():return self generator 屬於 iterator,但反之則不成立 使用yield表達式實作__next__()
https://leetcode.com/problems/letter-case-permutation/ Solution Time Complexity = Space Complexity = O(len(s) * 2 ^ len(s)) class Solution: def letterCasePermutation(self, s: str) -> List[str]: f = ...