前端面试常见基础算法

一、数组与字符串处理

  1. 两数之和(哈希表)
    题目:找出数组中两数之和等于目标值的索引。
    代码示例(时间复杂度 O(n)):

    const twoSum = (nums, target) => {
      const map = new Map();
      for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (map.has(complement)) return [map.get(complement), i];
        map.set(nums[i], i);
      }
    };
    
  2. 最长无重复子串(滑动窗口)
    题目:求字符串中最长不重复字符的子串长度。
    代码示例(时间复杂度 O(n)):

    const lengthOfLongestSubstring = (s) => {
      let map = new Map(), max = 0, left = 0;
      for (let right = 0; right < s.length; right++) {
        if (map.has(s[right])) left = Math.max(left, map.get(s[right]) + 1);
        map.set(s[right], right);
        max = Math.max(max, right - left + 1);
      }
      return max;
    };
    

二、链表操作

  1. 反转链表(迭代法)
    代码示例(空间复杂度 O(1)):

    function reverseList(head) {
      let prev = null, curr = head;
      while (curr) {
        const next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
      }
      return prev;
    }
    
  2. 合并两个有序链表(递归)
    代码示例(时间复杂度 O(m+n)):

    const mergeTwoLists = (l1, l2) => {
      if (!l1) return l2;
      if (!l2) return l1;
      if (l1.val < l2.val) {
        l1.next = mergeTwoLists(l1.next, l2);
        return l1;
      } else {
        l2.next = mergeTwoLists(l1, l2.next);
        return l2;
      }
    };
    

三、排序与搜索

  1. 快速排序(分治法)
    代码示例(平均时间复杂度 O(n log n)):

    const quickSort = (arr) => {
      if (arr.length <= 1) return arr;
      const pivot = arr.pop();
      const left = arr.filter(x => x <= pivot);
      const right = arr.filter(x => x > pivot);
      return [...quickSort(left), pivot, ...quickSort(right)];
    };
    
  2. 数组扁平化与去重
    代码示例(ES6 语法优化):

    const flattenAndUnique = (arr) => 
      [...new Set(arr.flat(Infinity))].sort((a, b) => a - b);
    

四、动态规划与贪心

  1. 爬楼梯问题(滚动数组)
    题目:每次爬 1 或 2 阶,到第 n 阶有多少种方法?
    代码示例(时间复杂度 O(n),空间 O(1)):

    const climbStairs = (n) => {
      let [a, b] = [1, 1];
      for (let i = 2; i <= n; i++) [a, b] = [b, a + b];
      return b;
    };
    
  2. 买卖股票的最佳时机(贪心)
    代码示例(一次遍历):

    const maxProfit = (prices) => {
      let min = Infinity, max = 0;
      for (const price of prices) {
        min = Math.min(min, price);
        max = Math.max(max, price - min);
      }
      return max;
    };
    

五、设计类问题

  1. 实现 Promise.all
    代码示例(处理异步并发):
    Promise.myAll = (promises) => {
      return new Promise((resolve, reject) => {
        let results = [], count = 0;
        promises.forEach((p, i) => {
          p.then(res => {
            results[i] = res;
            if (++count === promises.length) resolve(results);
          }).catch(reject);
        });
      });
    };
    

六、其他高频题

  1. 有效括号(栈)
    题目:判断括号字符串是否有效。
  2. 二叉树的层序遍历(BFS)
    使用队列实现层级遍历。
  3. LRU 缓存(Map + 双向链表)
    实现 O(1) 时间的 get/put 操作。

考察重点
复杂度分析:需明确时间/空间复杂度,如快速排序的最坏情况是 O(n²)。
边界处理:如链表操作中的空指针、数组越界等。
代码简洁性:优先使用 ES6+ 语法(如 Map、解构赋值)。
前端结合点:如虚拟 DOM Diff 算法、DOM 遍历优化等。

更多完整题目及实现可参考上述来源文档。

posted @ 2025-03-04 09:27  脆皮鸡  阅读(176)  评论(0)    收藏  举报