- 手写 快排 - pre
- 手写大整数 减法 - 1
- 手写 debounce throttle -2
code.juejin.cn/api/raw/742…
function debounce(func, sec) {
let timeout;
return function() {
const args = arguments;
const that = this;
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(that, args);
}, sec);
}
}
function throttle(func, sec) {
let timeout;
return function () {
const that = this;
const args = arguments;
if (!timeout) {
func.apply(that, args);
timeout = true;
setTimeout(() => {
timeout = false;
}, sec);
}
}
}
const newB = throttle(a, 1000);
newB(1);
newB(12);
- 反转一个 stack 「利用递归」-2
- 手写 判断一个树是不是另一个树的子树 -3
function isSameTree(A, B) {
if(A === null && B === null) {
return true;
}
if (A === null || A.val !== B.val) {
return false;
}
if (B === null) {
return true;
}
return isSameTree(A.left, B.left) && isSameTree(A.right, B.right);
}
function isSubTree(A, B) {
if (A === null) {
return false;
}
if(isSameTree(A, B) || isSubTree(A.left, B) || isSubTree(A.right, B)) {
return true;
}
return false;
}