js经典算法面试题(附答案)
最佳答案 问答题库858位专家为你答疑解惑
- 两数之和
题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
function 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);}return [];
}
- 反转字符串
题目:给你一个字符串 s,请你将它反转成一个新的字符串。
function reverseString(s) {return s.split('').reverse().join('');
}
- 最长公共子序列
题目:给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
function longestCommonSubsequence(text1, text2) {const m = text1.length;const n = text2.length;const dp = Array.from({ length: m + 1 }, () => 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];
}
- 合并两个有序数组
题目:给你两个有序整数数组 nums1 和 nums2,请你将它们合并成一个的新数组。你可以假设这个新数组的长度一定小于或等于 nums1.length + nums2.length。
function mergeTwoArrays(nums1, nums2) {const result = [];let i = 0;let j = 0;while (i < nums1.length && j < nums2.length) {if (nums1[i] < nums2[j]) {result.push(nums1[i]);i++;} else {result.push(nums2[j]);j++;}}return result.concat(nums1.slice(i)).concat(nums2.slice(j));
}
- 三数之和
题目:给你一个包含 n 个整数的数组 nums,判断是否可以通过在数组中选择一个起始点,然后向后选择两个整数,使得这三个整数的和等于目标值。
function threeSum(nums, target) {const result = [];nums.sort((a, b) => a - b);for (let i = 0; i < nums.length - 2; i++) {if (i > 0 && nums[i] === nums[i - 1]) continue;const left = i + 1;const right = nums.length - 1;while (left < right) {const sum = nums[i] + nums[left] + nums[right];if (sum === target) {result.push([nums[i], nums[left], nums[right]]);left++;right--;while (left < right && nums[left] === nums[left - 1]) left++;while (left < right && nums[right] === nums[right + 1]) right--;} else if (sum < target) {left++;} else {right--;}}}return result;
}
- 四数之和
题目:给你一个包含 n 个整数的数组 nums,判断是否可以使用其中的四个数来组成一个矩形。如果可以,返回 true;否则,返回 false。
function fourSum(nums, target) {const result = [];nums.sort((a, b) => a - b);for (let i = 0; i < nums.length - 3; i++) {if (i > 0 && nums[i] === nums[i - 1]) continue;for (let j = i + 1; j < nums.length - 2; j++) {if (j > i + 1 && nums[j] === nums[j - 1]) continue;const left = j + 1;const right = nums.length - 1;while (left < right) {const sum = nums[i] + nums[j] + nums[left] + nums[right];if (sum === target) {result.push([nums[i], nums[j], nums[left], nums[right]]);left++;right--;while (left < right && nums[left] === nums[left - 1]) left++;while (left < right && nums[right] === nums[right + 1]) right--;} else if (sum < target) {left++;} else {right--;}}}}return result;
}
- 两数之和 II - 输入有序数组
题目:给定一个已排序的整数数组 nums,找出两个数满足它们的和等于目标值 target。你只能使用每个元素一次。
function twoSumSorted(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);}return [];
}
- 最大矩形面积
题目:给定一个二维数组 heights,其中 heights[i] 表示第 i 行的高度,返回能够勾勒出的最大矩形面积。
function largestRectangleArea(heights) {const stack = [];heights.push(0);let maxArea = 0;for (let i = 0; i < heights.length; i++) {while (stack.length && heights[stack[stack.length - 1]] > heights[i]) {const height = heights[stack.pop()];const width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1;maxArea = Math.max(maxArea, height * width);}stack.push(i);}return maxArea;
}
- 盛最多水的容器
题目:给定 n 个非负整数 a1、a2、…、an,请计算连乘这些整数所得到的积,并以字符串形式输出。
function multiply(nums) {let result = '1';for (const num of nums) {result = BigInt(result) * BigInt(num);}return result.toString();
}
- 单词拆分 II
题目:给定一个非空字符串 s 和一个定义好的分隔符集合 nonWordChars,返回所有可能的句子排列。句子是由空格分隔的单词组成的。换句话说,要生成所有可能的句子,可以使用回溯算法进行求解。
function wordBreak(s, wordDict) {const result = [];const memo = new Map();function backtrack(start) {if (memo.has(start)) {return memo.get(start);}if (start === s.length) {result.push('');return;}for (let end = start + 1; end <= s.length; end++) {if (wordDict.has(s.slice(start, end))) {backtrack(end);if (end !== start + 1) {const sentences = result.pop();result.push(sentences + ' ' + s.slice(start, end));} else {result.push(s.slice(start, end));}}}memo.set(start, result);return result;}backtrack(0);return result;
}
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"js经典算法面试题(附答案)":http://eshow365.cn/6-41501-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!