- 用set
- 排序后从第一项开始跟前一项比较 (最慢)
- 用 python collections.Counter 统计出现次数 (次快,可以知道出现次数)
- 用一个 boolean 列表来看看这个数字是否见过 (最快,但是无法知道出现次数)
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
# method 1: setclass Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
# method 1: setSolution 1: Iterative
Time complexity: O(n)
Space complexity: O(1)
class Solution
{
public int fib(int N)
{| target_num = 0 | |
| length = 100 | |
| # two methods | |
| # 1 | |
| a = [target_num for x in range(length)] | |
| # 2 | |
| b = [target_num] * length |
| #从一个列表中提取每一行的列作为新的列表 | |
| a=["cba","daf","ghi"] | |
| a=list(zip(*a)) | |
| print(a) # [('c', 'd', 'g'), ('b', 'a', 'h'), ('a', 'f', 'i')] |
| from functools import wraps | |
| from time import time | |
| def timing(f): | |
| @wraps(f) | |
| def wrap(*args, **kw): | |
| ts = time() | |
| result = f(*args, **kw) | |
| te = time() | |
| print( |
| import cv2 | |
| import numpy as np | |
| import base64 | |
| image = "" # raw data with base64 encoding | |
| decoded_data = base64.b64decode(image) | |
| np_data = np.fromstring(decoded_data,np.uint8) | |
| img = cv2.imdecode(np_data,cv2.IMREAD_UNCHANGED) | |
| cv2.imshow("test", img) | |
| cv2.waitKey(0) |
| class Solution: | |
| def increasingBST(self, root): | |
| def inorder(node): | |
| if node: | |
| # 这里用了yield 和 yield from | |
| # yield from 作为递归 | |
| # yield 返回值 | |
| # 这是一个左中右的 inorder 遍历 | |
| # 返回一个 generator | |
| yield from inorder(node.left) |