https://leetcode.com/contest/biweekly-contest-109/problems/visit-array-positions-to-maximize-score/ Solution Time Complexity: O(len(nums)) Space Complexity: O(1) (The input and output generally do not ...
https://leetcode.com/contest/biweekly-contest-109/problems/ways-to-express-an-integer-as-sum-of-powers/ Solution 當你需要的是出現的次數而無關順序時 ⇒ 使用 list Counter Time Complexity: O() Space Complexity: O() (The inp ...
計算value出現的次數 list.count(value) string.count(value, start, end)
Traversal 遍歷 (Using Binary Tree) 請參考:Binary Tree 二元樹 – Traversal 遍歷 範例 Leetcode # 439. Ternary Expression Parser Using Stack 一般來說,優先度相同時,優先順序從左至右 而當優先順序從右至左時,要改為逆序存取 (範例:Leetcode # 439. Ternary ...
Metasyntactic Variable foo 和 bar
lst = [1, 2, 3] def foo(): lst.append(5) # OK lst += [5] # ERROR here foo() lst += [5]≡lst = lst + [5] In Python, variables that are only referenced inside a function are implicitly global. If a varia ...
https://leetcode.com/problems/knight-probability-in-chessboard Solution: Top-down Dynamic Programming (Memoization) Time Complexity: O(k * n * n) Space Complexity: O(k * n * n) (The input and output g ...
zip() zip(*iterables, strict=False) zip() returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument iterables. Like: zip([1, 2, 3], ['sugar', 'spice', 'ev ...
Unpacking x, y, z = coordinates *iterable print(1, *[2, 3]) ⇒ 1, 2, 3 print([*[1, 2], *[3, 4]]) ⇒ 1, 2, 3, 4 first, second, *other = customers | first, *middle, last = name dict_3 = {**dict_1, **dict_ ...
https://leetcode.com/problems/string-to-integer-atoi Solution Time Complexity: O(len(s)) Space Complexity: O(1) (The input and output generally do not count towards the space complexity.) class Soluti ...