Administrator
Published on 2026-01-28 / 0 Visits
0
0

leetcode question with little to record

56. 合并区间 - 力扣(LeetCode)

  1. Python sortAPI Sorting Techniques — Python 3.14.2 documentation

  2. lambda 6. Expressions — Python 3.14.2 documentation

0x3F also need to special check if ans is None, it is essencial

class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        intervals.sort(key = lambda interval: interval[0]) # sort in place
        if not intervals:
            return []
        ans = [intervals[0]]
        for interval in intervals:
            # only see the last one with interval, ensure the correctness with induction
            # integrate or append
            if ans[-1][1] >= interval[0]:
                ans[-1][1] = max(ans[-1][1], interval[1])
            else:
                ans.append(interval)
        return ans 
        

543. 二叉树的直径 - 力扣(LeetCode)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        ans = 0
        def maxDepth(root) -> int:
            if root is None:
                return -1 # leaf is 0, then None should be -1
            DepthLeft = maxDepth(root.left) + 1 # count from left to root 
            DepthRight = maxDepth(root.right) + 1  
            nonlocal ans 
            ans = max(ans, DepthLeft + DepthRight)
            return max(DepthLeft, DepthRight)
        maxDepth(root)
        return ans
        
        

Non-local

excerpt from 9. Classes — Python 3.14.2 documentation

The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function. (Actually, forgetting would be a better way to describe what actually happens.) Of course, recursive invocations each have their own local namespace.

A scope is a textual region of a Python program where a namespace is directly accessible. “Directly accessible” here means that an unqualified reference to a name attempts to find the name in the namespace.

  • directly accessible:就是你直接写 x,而不是写 math.x 或者 self.x。

  • unqualified reference:不带点号(.)的变量名。

Although scopes are determined statically, they are used dynamically. At any time during execution, there are 3 or 4 nested scopes whose namespaces are directly accessible:

  • the innermost scope, which is searched first, contains the local names

  • the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contain non-local, but also non-global names

  • the next-to-last scope contains the current module’s global names

  • the outermost scope (searched last) is the namespace containing built-in names

解释(LEGB 法则):

当你在代码里用了一个变量名 x,Python 会像剥洋葱一样,从内向外一层层找:

L (Local): 你的函数内部。

E (Enclosing): 如果你在函数里又写了一个函数,那外层函数的区域就是 Enclosing。

G (Global): 当前 .py 文件定义的全局变量。

B (Built-in): Python 自带的函数,比如 len, print, int。

If a name is declared global, then all references and assignments go directly to the next-to-last scope containing the module’s global names. To rebind variables found outside of the innermost scope, the nonlocal statement can be used; if not declared nonlocal, those variables are read-only ( an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged).

只要你想在函数内部对一个不在当前函数定义的变量进行“赋值操作”(即使用 = 修改指向),你就必须先在函数内用一行代码单独声明它。


Comment