Skip to content

Instantly share code, notes, and snippets.

View HoweChen's full-sized avatar
🎯
Focusing

Yuhao.Chen (Howe) - 陈雨豪 HoweChen

🎯
Focusing
View GitHub Profile
@HoweChen
HoweChen / inorder_BST.py
Last active March 14, 2019 08:50
[inorderBST]using yield and yiled from #Python #Algorithm
class Solution:
def increasingBST(self, root):
def inorder(node):
if node:
# 这里用了yield 和 yield from
# yield from 作为递归
# yield 返回值
# 这是一个左中右的 inorder 遍历
# 返回一个 generator
yield from inorder(node.left)
@HoweChen
HoweChen / opencv_from_base64.py
Last active March 19, 2024 20:20
[opencv_from_base64]read image from opencv with base64 encoding #OpenCV
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)
@HoweChen
HoweChen / python_timing_wrapper.py
Last active February 20, 2019 09:25
[Python timing wrapper]The python timing wrapper #Python
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(
@HoweChen
HoweChen / Python extract column to a new list.py
Created March 6, 2019 13:26
[Python extract column to a new list]Python extract Column to a new list #Python
#从一个列表中提取每一行的列作为新的列表
a=["cba","daf","ghi"]
a=list(zip(*a))
print(a) # [('c', 'd', 'g'), ('b', 'a', 'h'), ('a', 'f', 'i')]
@HoweChen
HoweChen / list_with_initNum.py
Created March 10, 2019 07:41
[Initiate new list with default value]Initiage new list with default value #Python
target_num = 0
length = 100
# two methods
# 1
a = [target_num for x in range(length)]
# 2
b = [target_num] * length
@HoweChen
HoweChen / Solution.md
Created March 10, 2019 07:50
[斐波那契数列几种解法] #Algorithm

Solution 1: Iterative

Time complexity: O(n) Space complexity: O(1)

class Solution
{
    public int fib(int N)
    {
@HoweChen
HoweChen / 列表里寻找重复项.md
Created March 13, 2019 06:45
[列表操作] #Algorithm
  1. 用set
  2. 排序后从第一项开始跟前一项比较 (最慢)
  3. 用 python collections.Counter 统计出现次数 (次快,可以知道出现次数)
  4. 用一个 boolean 列表来看看这个数字是否见过 (最快,但是无法知道出现次数)
class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        # method 1: set
@HoweChen
HoweChen / bidirection.py
Created March 14, 2019 08:52
[一个for循环实现双向遍历]一个for循环实现双向遍历 #Python
from itertools import chain # 这里要用itertool里的chain
# 因为在python2里 range() 返回list
# python3 里range 返回iterator,不能直接相加,要用chain方法
for i in chain(range(n), range(n)[::-1]): # 这个写法可以实现一个for里双向遍历
@HoweChen
HoweChen / get_true_count_list.py
Created March 20, 2019 06:48
[统计boolean列表里正确的结果] #Python
test = [True,True,False,False]
print(sum(test)) # 2
@HoweChen
HoweChen / monotonic_array.py
Created March 28, 2019 08:32
[Monotonic Array 连续性列表 ]Check if Python List is monotone increasing or decreasing #Python
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)