返回

Leet Code 刷题宝典:15 个核心 JS 函数 助你从入门到精通

前端

在 Leet Code 的世界里,JavaScript 是我们的魔法棒。掌握了核心的 15 个函数,你就能如鱼得水,轻松应对各种挑战。今天,就让我们一起揭开这些函数的神秘面纱,看看它们是如何帮助我们解决问题的。

一、数据结构和算法:数组的巧妙运用

1.1 Array.prototype.sort()

sort() 方法用于对数组的元素进行排序。默认情况下,它会按照字符串 Unicode 码位顺序进行排序。

const arr = [1, 2, 3, 4, 5];
const sortedArr = arr.sort((a, b) => a - b); // [1, 2, 3, 4, 5]

1.2 Array.prototype.filter()

filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。

const arr = [1, 2, 3, 4, 5];
const filteredArr = arr.filter(num => num % 2 === 0); // [2, 4]

1.3 Array.prototype.map()

map() 方法创建一个新数组,其结果是调用提供的函数在每个元素上的结果。

const arr = [1, 2, 3, 4, 5];
const mappedArr = arr.map(num => num * 2); // [2, 4, 6, 8, 10]

1.4 Array.prototype.reduce()

reduce() 方法对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值。

const arr = [1, 2, 3, 4, 5];
const reducedValue = arr.reduce((acc, num) => acc + num, 0); // 15

二、函数式编程:简洁高效的代码

2.1 箭头函数

箭头函数提供了一种更简洁的函数语法。

const doubled = num => num * 2;

2.2 Function.prototype.bind()

bind() 方法创建一个新的函数,当被调用时,其 this 关键字设置为提供的值。

const boundFunction = doubled.bind(this); // 绑定 this 上下文
boundFunction(5); // 10

2.3 Function.prototype.call()

call() 方法调用一个具有给定 this 值的函数,以及作为一个数组(或类似数组对象)提供的参数。

boundFunction.call(this, [5]); // 10

2.4 Function.prototype.apply()

apply() 方法与 call() 类似,但它接受一个数组作为参数列表。

boundFunction.apply(this, [5]); // 10

三、字符串操作:文本处理的艺术

3.1 String.prototype.slice()

slice() 方法返回一个新字符串,其中包含源字符串从开始到结束(不包括结束)的所有字符。

const str = "Hello, Leet Code!";
const slicedStr = str.slice(7); // "Leet Code!"

3.2 String.prototype.substring()

substring() 方法返回一个新字符串,其中包含源字符串从开始到结束(包括结束)的所有字符。

const str = "Hello, Leet Code!";
const substringStr = str.substring(7, 12); // "Leet"

3.3 String.prototype.charAt()

charAt() 方法返回在指定位置的字符。

const str = "Hello, Leet Code!";
const charAtStr = str.charAt(0); // "H"

3.4 String.prototype.charCodeAt()

charCodeAt() 方法返回在指定位置字符的 Unicode 代码。

const str = "Hello, Leet Code!";
const charCodeStr = str.charCodeAt(0); // 72

四、数学运算:精确的计算工具

4.1 Math.abs()

abs() 方法返回数字的绝对值。

const num = -5.5;
const absNum = Math.abs(num); // 5.5

4.2 Math.ceil()

ceil() 方法返回大于或等于给定数字的最小整数。

const num = -5.5;
const ceilNum = Math.ceil(num); // -5

4.3 Math.floor()

floor() 方法返回小于或等于给定数字的最大整数。

const num = -5.5;
const floorNum = Math.floor(num); // -6

4.4 Math.round()

round() 方法返回最接近的整数。

const num = -5.5;
const roundNum = Math.round(num); // -6

五、日期和时间操作:掌控时间的力量

5.1 Date.now()

Date.now() 方法返回当前时间戳。

const date = new Date();
const timestamp = date.getTime(); // 当前时间戳

5.2 Date.parse()

parse() 方法将一个表示日期的字符串转换为时间戳。

const date = new Date("2023-01-01");
const timestamp = Date.parse(date.toISOString()); // 1672531200000

5.3 new Date()

new Date() 创建一个新的 Date 对象,表示当前日期和时间。

const date = new Date();

5.4 Date.prototype.getFullYear()

getFullYear() 方法返回年份。

const date = new Date();
const year = date.getFullYear(); // 当前年份

5.5 Date.prototype.getMonth()

getMonth() 方法返回月份(0-11)。

const date = new Date();
const month = date.getMonth(); // 当前月份

5.6 Date.prototype.getDate()

getDate() 方法返回月份中的某一天(1-31)。

const date = new Date();
const day = date.getDate(); // 当前日期

通过掌握这些核心的 JavaScript 函数,你将能够更高效地解决 Leet Code 上的各种问题。记住,实践是掌握这些函数的最好方式。不断练习,你会发现它们的威力。祝你在 Leet Code 的旅程中一帆风顺!