2024.10 Microsoft 面试问题记录

70 阅读1分钟
  1. 手写 快排 - pre
  2. 手写大整数 减法 - 1
  3. 手写 debounce throttle -2 code.juejin.cn/api/raw/742…
// 文本 两个参数 func, 时间 wait textinput

function debounce(func, sec) {
    let timeout;

    return function() {
        const args = arguments;
        const that = this;
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            func.apply(that, args);
        }, sec);
    }
}
// const a = (s) => console.log(s);
// const newA = debounce(a, 1000);
// newA(1);
// newA(12);

// 表格
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);
  1. 反转一个 stack 「利用递归」-2
  2. 手写 判断一个树是不是另一个树的子树 -3
// A B
/*
 5
4 3

    5
   1 2
  2 5 4 5
 1 4 3
      1
*/

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;
    }
    // 当前的rootA 与 rootB 值 == && 不为空, 递归左右子树
    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;
    }

    // 递归结束,没找到 same 返回 false
    return false;
}