本文共 768 字,大约阅读时间需要 2 分钟。
题目:(简单)
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | – | – | 52ms (66.97%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(使用threading.Lock实现):
from threading import Lockclass Foo: def __init__(self): self.firstJobDone = Lock() self.secondJobDone = Lock() self.firstJobDone.acquire() self.secondJobDone.acquire() def first(self, printFirst: 'Callable[[], None]') -> None: printFirst() self.firstJobDone.release() def second(self, printSecond: 'Callable[[], None]') -> None: with self.firstJobDone: printSecond() self.secondJobDone.release() def third(self, printThird: 'Callable[[], None]') -> None: with self.secondJobDone: printThird()
转载地址:http://gmgdf.baihongyu.com/