Skip to content

Commit 1a2b5b7

Browse files
committed
week14: add three js solution
1 parent ce82bb7 commit 1a2b5b7

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@
281281
|163|[Missing Ranges](https://leetcode.com/problems/missing-ranges/) ♥ | |Medium|
282282
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/) | |Medium|
283283
|161|[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)♥ | |Medium|
284-
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [java](./algorithms/intersectionOfTwoLinkedLists/Solution.java) |Easy|
284+
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [java](./algorithms/intersectionOfTwoLinkedLists/Solution.java), [js](./algorithms/intersectionOfTwoLinkedLists/Solution.js) |Easy|
285285
|159|[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | |Hard|
286286
|158|[Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) ♥ | |Hard|
287287
|157|[Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) ♥ | |Easy|
@@ -363,7 +363,7 @@
363363
|81|[Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)| |Medium|
364364
|80|[Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| |Medium|
365365
|79|[Word Search](https://leetcode.com/problems/word-search/)| |Medium|
366-
|78|[Subsets](https://leetcode.com/problems/subsets/)| |Medium|
366+
|78|[Subsets](https://leetcode.com/problems/subsets/)| [js](./algorithms/subsets/Solution.js) |Medium|
367367
|77|[Combinations](https://leetcode.com/problems/combinations/)| |Medium|
368368
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)| |Hard|
369369
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)| |Medium|
@@ -395,7 +395,7 @@
395395
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| |Medium|
396396
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| |Medium|
397397
|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)| |Hard|
398-
|46|[Permutations](https://leetcode.com/problems/permutations/)| |Medium|
398+
|46|[Permutations](https://leetcode.com/problems/permutations/)| [js](./algorithms/permutations/Solution.js) |Medium|
399399
|45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)| |Hard|
400400
|44|[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| |Hard|
401401
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)| |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var getIntersectionNode = function(headA, headB) {
2+
if (!headA || !headB) return null;
3+
4+
let pA = headA,
5+
pB = headB;
6+
// 相等只有2种情况:
7+
// 1. 两者相交 2. 都走到了最后
8+
while (pA !== pB) {
9+
// 如果没走到最后,就往后走一步;否则就指向互相的头节点
10+
pA = pA === null ? headB : pA.next;
11+
pB = pB === null ? headA : pB.next;
12+
}
13+
return pA;
14+
};

algorithms/permutations/Solution.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var permute = function (nums) {
2+
const res = [];
3+
const backtrack = (path) => {
4+
// 3. 递归的终点
5+
if(path.length === nums.length) {
6+
res.push(path);
7+
return;
8+
}
9+
// 遍历所有数字
10+
nums.forEach(n => {
11+
// 2. 如果包含当前数字,也就是重复了,即死路,就不要递归了
12+
if (path.includes(n)) return;
13+
// 1. 把数字加到数组里,进行递归调用
14+
backtrack(path.concat(n));
15+
})
16+
};
17+
backtrack([]);
18+
return res;
19+
};

algorithms/subsets/Solution.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var subsets = function (nums) {
2+
const res = [];
3+
const backtrack = (path, l, start) => {
4+
if (path.length === l) {
5+
res.push(path);
6+
return;
7+
}
8+
// for循环遍历数组中的所有数字
9+
// 没有用forEach来遍历是因为要保证子集的有序性,后面接的数字必须是当前数字后面的数字
10+
// 因此还需要记录一个下标来
11+
for (let i = start; i < nums.length; i++) {
12+
backtrack(path.concat(nums[i]), l, i + 1);
13+
}
14+
};
15+
// 在for循环中进行回溯的调用
16+
for (let i = 0; i <= nums.length; i++) {
17+
backtrack([], i, 0);
18+
}
19+
return res;
20+
};

0 commit comments

Comments
 (0)