Solution 1: Iterative
Time complexity: O(n)
Space complexity: O(1)
class Solution
{
public int fib(int N)
{| class Solution: | |
| def increasingBST(self, root): | |
| def inorder(node): | |
| if node: | |
| # 这里用了yield 和 yield from | |
| # yield from 作为递归 | |
| # yield 返回值 | |
| # 这是一个左中右的 inorder 遍历 | |
| # 返回一个 generator | |
| yield from inorder(node.left) |
| 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) |
| 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( |
| #从一个列表中提取每一行的列作为新的列表 | |
| a=["cba","daf","ghi"] | |
| a=list(zip(*a)) | |
| print(a) # [('c', 'd', 'g'), ('b', 'a', 'h'), ('a', 'f', 'i')] |
| target_num = 0 | |
| length = 100 | |
| # two methods | |
| # 1 | |
| a = [target_num for x in range(length)] | |
| # 2 | |
| b = [target_num] * length |
Solution 1: Iterative
Time complexity: O(n)
Space complexity: O(1)
class Solution
{
public int fib(int N)
{class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
# method 1: set| from itertools import chain # 这里要用itertool里的chain | |
| # 因为在python2里 range() 返回list | |
| # python3 里range 返回iterator,不能直接相加,要用chain方法 | |
| for i in chain(range(n), range(n)[::-1]): # 这个写法可以实现一个for里双向遍历 |
| test = [True,True,False,False] | |
| print(sum(test)) # 2 |
| def increasing(A:List[int])->bool: | |
| return all(x<=y for x,y in zip(A,A[1::])) | |
| def decreasing(A:List[int])->bool: | |
| return all(x>=y for x,y in zip(A,A[1::])) | |
| #如果要检查是否是连续上升或下降,就用 or 把两个连起来 | |
| def isMonotonic(A: List[int]) -> bool: | |
| return self.increasing(A) or self.decreasing(A) |