https://leetcode.com/problems/reverse-linked-list-ii/ Solution 注意和 Leetcode # 206. Reverse Linked List 相同的多重賦值順序問題 Time Complexity: O(n) Space Complexity: O(1) class Solution: def reverseBetween(self, ...
用一個等號同時為多個變數賦值 x = a, y = b x, y = a, b 計算順序 a, b = c, d 計算 c, d 賦值 c 給 a 賦值 d 給 b 實現「數值交換 (Swap)」 a, b = b, a 因為在賦值給 a, b 之前,已經先計算過了 b, a 所以在賦值給 b 時,不會因為 a 已經改變過數值而被影響 因為計算順序而出錯的範例 Leetcode # 203. ...
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 ...