leetcode刷题 js版

leetcode刷题 js版

刷题地址在这leetcode

当然网址上也会有java版的解答, 涉及优化的细节

1. Two Sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
// Approach 1: Brute Force
var twoSum = function(nums, target) {
for( let i = 0; i < nums.length; i++) {
for(let j = i+1; j < nums.length; j++) {
if (nums[j] === target - nums[i]) {
return [i,j]
}
}
}
};
// Approach 2: Two-pass Hash Table
var twoSum = function(nums, target) {
const map = new Map()
for(let i = 0; i < nums.length; i++) {
map.set(nums[i], i);
}
for(let i = 0; i < nums.length; i++) {
let complement = target - nums[i];
if (map.has(complement) && map.get(complement) != i) {
return [map.get(complement), i]
}
}
};
// Approach 3: One-pass Hash Table
var twoSum = function(nums, target) {
const map = new Map()
for(let i = 0; i < nums.length; i++) {
let complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i]
}
map.set(nums[i], i)
}
};

2. Add Two Numbers

5. Longest Palindromic Substring

参考

LeetCode solutions with JavaScript
chihungyu1116/leetcode-javascript
Demonstrate all the questions on LeetCode in the form of animation.