西维蜀黍的OJ Blog

🐒 Software engineer | 📷 Photographer | 👹 Urban explorer

[Note] Bit Manipulation

解析 268. Missing Number # use space - hashset # - time: O(n) # - space: O(1) def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ s = set() for num in nums: s.add(num) for v in range(0, len(nums) + 1): if v not in s: return v # XOR(异或) # 3 ^ 3 -> 0(相同元素会   ...


[Note] Interval

解析 57. Insert Interval # greedy (myway and neetcode) # - time: O(n) # - space: O(1), 除了存储返回答案的空间以外,我们只需要额外的常数空间即可。 def insert(self, intervals, newInterval): """ :type intervals: List[List[int]] :type newInterval: List[int] :rtype: List[List[int]] """ res = [] for i, interval in enumerate(intervals): if   ...


[Note] Math

解析 41. First Missing Positive # - time: O(n) # - space: O(n) def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ s = set() for num in nums: if num > 0: s.add(num) for n in range(1, len(nums) + 2): # e.g., [1, 2] if n not in s: return n # Boolean Array # 用一个 boolean array 记录下 [1, len(nums)] 的范   ...


[Note] Matrix

解析 54. Spiral Matrix # - time: O(m*n) # - space: O(m*n) -> O(1) extra space, and O(m*n) space for the output list def spiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ # current valid search range: col: [minCol, maxCol], row: [minRow, maxRow] # this means that "while minRow <= maxRow and minCol <= maxCol" minRow, maxRow = 0, len(matrix) - 1 minCol, maxCol = 0, len(matrix[0]) -   ...


[Notes] 1-D DP

  ...