返回

JavaScript算法面试题精编

前端


如今,JavaScript已成为前端开发的主流语言,其应用场景广泛。算法是计算机科学的重要组成部分,也是软件开发的基础。在JavaScript开发中,算法的运用无处不在。掌握JavaScript算法,不仅有助于您理解JavaScript语言的运行机制,还能提高您解决问题的能力。

1. 两数之和

题目: 给定一个整数数组nums和一个目标值target,请找出两个数字的索引,使得它们的和等于target。

解答:

function twoSum(nums, target) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j];
      }
    }
  }
  return null;
}

// 测试
console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1]
console.log(twoSum([1, 3, 4, 6], 5)); // [0, 2]
console.log(twoSum([3, 3], 6)); // [0, 1]

2. 无重复字符的最长子串

题目: 给定一个字符串s,请找出其中最长不包含重复字符的子串。

解答:

function longestSubstringWithoutRepeatingCharacters(s) {
  let maxLen = 0;
  let left = 0;
  let right = 0;
  let charMap = {};

  while (right < s.length) {
    const char = s[right];
    if (!charMap[char]) {
      charMap[char] = true;
      maxLen = Math.max(maxLen, right - left + 1);
      right++;
    } else {
      delete charMap[s[left]];
      left++;
    }
  }

  return maxLen;
}

// 测试
console.log(longestSubstringWithoutRepeatingCharacters("abcabcbb")); // 3
console.log(longestSubstringWithoutRepeatingCharacters("bbbbb")); // 1
console.log(longestSubstringWithoutRepeatingCharacters("pwwkew")); // 3

3. 最长公共子序列

题目: 给定两个字符串text1和text2,请找出它们的最长公共子序列。

解答:

function longestCommonSubsequence(text1, text2) {
  const m = text1.length;
  const n = text2.length;
  const dp = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));

  for (let i = 1; i <= m; i++) {
    for (let j = 1; j <= n; j++) {
      if (text1[i - 1] === text2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  return dp[m][n];
}

// 测试
console.log(longestCommonSubsequence("ABCD", "ACED")); // 3
console.log(longestCommonSubsequence("AGGTAB", "GXTXAYB")); // 4
console.log(longestCommonSubsequence("HARRY", "SALLY")); // 2

4. 归并排序

题目: 给定一个整数数组nums,请使用归并排序对其进行排序。

解答:

function mergeSort(nums) {
  if (nums.length <= 1) {
    return nums;
  }

  const mid = Math.floor(nums.length / 2);
  const left = mergeSort(nums.slice(0, mid));
  const right = mergeSort(nums.slice(mid));

  return merge(left, right);
}

function merge(left, right) {
  const result = [];
  let i = 0;
  let j = 0;

  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      result.push(left[i]);
      i++;
    } else {
      result.push(right[j]);
      j++;
    }
  }

  while (i < left.length) {
    result.push(left[i]);
    i++;
  }

  while (j < right.length) {
    result.push(right[j]);
    j++;
  }

  return result;
}

// 测试
console.log(mergeSort([5, 3, 1, 2, 4])); // [1, 2, 3, 4, 5]
console.log(mergeSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(mergeSort([])); // []

5. 快速排序

题目: 给定一个整数数组nums,请使用快速排序对其进行排序。

解答:

function quickSort(nums) {
  if (nums.length <= 1) {
    return nums;
  }

  const pivot = nums[0];
  const left = [];
  const right = [];

  for (let i = 1; i < nums.length; i++) {
    if (nums[i] < pivot) {
      left.push(nums[i]);
    } else {
      right.push(nums[i]);
    }
  }

  return quickSort(left).concat(pivot, quickSort(right));
}

// 测试
console.log(quickSort([5, 3, 1, 2, 4])); // [1, 2, 3, 4, 5]
console.log(quickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(quickSort([])); // []

结论

以上列举的只是众多JavaScript算法面试题中的一部分。通过解决这些问题,您可以全面了解JavaScript算法的各种类型和用法。在实际的面试中,您可能会遇到更复杂的题目,但这些基础算法是解决更复杂问题的基石。因此,扎实掌握这些基础知识至关重要。