feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
This commit is contained in:
committed by
GitHub
parent
0095583028
commit
ee1e8abd87
@ -3,6 +3,7 @@ id: 587d8257367417b2b2512c7b
|
||||
title: 将新元素添加到二叉搜索树
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: add-a-new-element-to-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -90,5 +91,85 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
inOrder() {
|
||||
if (!this.root) {
|
||||
return null;
|
||||
}
|
||||
var result = new Array();
|
||||
function traverseInOrder(node) {
|
||||
node.left && traverseInOrder(node.left);
|
||||
result.push(node.value);
|
||||
node.right && traverseInOrder(node.right);
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.add = function(element) {
|
||||
let current = this.root;
|
||||
if (!current) {
|
||||
this.root = new Node(element);
|
||||
return;
|
||||
} else {
|
||||
const searchTree = function(current) {
|
||||
if (current.value > element) {
|
||||
if (current.left) {
|
||||
return searchTree(current.left);
|
||||
} else {
|
||||
current.left = new Node(element);
|
||||
return;
|
||||
}
|
||||
} else if (current.value < element) {
|
||||
if (current.right) {
|
||||
return searchTree(current.right);
|
||||
} else {
|
||||
current.right = new Node(element);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
return searchTree(current);
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8252367417b2b2512c67
|
||||
title: 在链接列表中的特定索引处添加元素
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: add-elements-at-a-specific-index-in-a-linked-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -52,5 +53,105 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element) {
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function() {
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function() {
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element) {
|
||||
var node = new Node(element);
|
||||
if (head === null) {
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while (currentNode.next) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
length++;
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if (head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while (currentNode.next) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
length++;
|
||||
};
|
||||
this.addAt = function (index, element) {
|
||||
if (index > length || index < 0) {
|
||||
return false;
|
||||
}
|
||||
var newNode = new Node(element);
|
||||
var currentNode = head;
|
||||
if (index === 0) {
|
||||
head = newNode;
|
||||
} else {
|
||||
var previousNode = null;
|
||||
var i = 0;
|
||||
while (currentNode && i < index) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
i++;
|
||||
}
|
||||
previousNode.next = newNode;
|
||||
}
|
||||
newNode.next = currentNode;
|
||||
length++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8256367417b2b2512c77
|
||||
title: 邻接名单
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: adjacency-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -68,5 +69,21 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var undirectedAdjList = {};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var undirectedAdjList = {
|
||||
James: ['Jeff'],
|
||||
Jill: ['Jenny'],
|
||||
Jenny: ['Jill', 'Jeff'],
|
||||
Jeff: ['James', 'Jenny']
|
||||
};
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8256367417b2b2512c78
|
||||
title: 邻接矩阵
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: adjacency-matrix
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -88,5 +89,22 @@ assert(adjMatUndirected[2][4] === 1 && adjMatUndirected[4][2] === 1);
|
||||
assert(adjMatUndirected[3][4] === 1 && adjMatUndirected[4][3] === 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var adjMatUndirected = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var adjMatUndirected = [
|
||||
[0, 0, 1, 1, 0],
|
||||
[0, 0, 0, 0, 0],
|
||||
[1, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 1],
|
||||
[0, 0, 1, 1, 0]
|
||||
];
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825c367417b2b2512c90
|
||||
title: 广度优先搜索
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: breadth-first-search
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -81,5 +82,85 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
// Source: http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html
|
||||
function isEquivalent(a, b) {
|
||||
// Create arrays of property names
|
||||
var aProps = Object.getOwnPropertyNames(a);
|
||||
var bProps = Object.getOwnPropertyNames(b);
|
||||
// If number of properties is different,
|
||||
// objects are not equivalent
|
||||
if (aProps.length != bProps.length) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < aProps.length; i++) {
|
||||
var propName = aProps[i];
|
||||
// If values of same property are not equal,
|
||||
// objects are not equivalent
|
||||
if (a[propName] !== b[propName]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// If we made it this far, objects
|
||||
// are considered equivalent
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function bfs(graph, root) {
|
||||
var nodesLen = {};
|
||||
|
||||
return nodesLen;
|
||||
};
|
||||
|
||||
var exBFSGraph = [
|
||||
[0, 1, 0, 0],
|
||||
[1, 0, 1, 0],
|
||||
[0, 1, 0, 1],
|
||||
[0, 0, 1, 0]
|
||||
];
|
||||
console.log(bfs(exBFSGraph, 3));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bfs(graph, root) {
|
||||
var nodesLen = {};
|
||||
// Set all distances to infinity
|
||||
for (var i = 0; i < graph.length; i++) {
|
||||
nodesLen[i] = Infinity;
|
||||
}
|
||||
nodesLen[root] = 0; // ...except root node
|
||||
var queue = [root]; // Keep track of nodes to visit
|
||||
var current; // Current node traversing
|
||||
// Keep on going until no more nodes to traverse
|
||||
while (queue.length !== 0) {
|
||||
current = queue.shift();
|
||||
// Get adjacent nodes from current node
|
||||
var curConnected = graph[current]; // Get layer of edges from current
|
||||
var neighborIdx = []; // List of nodes with edges
|
||||
var idx = curConnected.indexOf(1); // Get first edge connection
|
||||
while (idx !== -1) {
|
||||
neighborIdx.push(idx); // Add to list of neighbors
|
||||
idx = curConnected.indexOf(1, idx + 1); // Keep on searching
|
||||
}
|
||||
// Loop through neighbors and get lengths
|
||||
for (var j = 0; j < neighborIdx.length; j++) {
|
||||
// Increment distance for nodes traversed
|
||||
if (nodesLen[neighborIdx[j]] === Infinity) {
|
||||
nodesLen[neighborIdx[j]] = nodesLen[current] + 1;
|
||||
queue.push(neighborIdx[j]); // Add new neighbors to queue
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodesLen;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8257367417b2b2512c7c
|
||||
title: 检查二进制搜索树中是否存在元素
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: check-if-an-element-is-present-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -88,5 +89,83 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return searchTree(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.isPresent = function (value) {
|
||||
var current = this.root
|
||||
while (current) {
|
||||
if (value === current.value) {
|
||||
return true;
|
||||
}
|
||||
current = value < current.value ? current.left : current.right;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5cc0c1b32479e176caf3b422
|
||||
title: Check if Tree is Binary Search Tree
|
||||
challengeType: 1
|
||||
forumTopicId: 301624
|
||||
dashedName: check-if-tree-is-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
@ -3,6 +3,7 @@ id: 587d8255367417b2b2512c75
|
||||
title: 创建循环队列
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-circular-queue
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -125,5 +126,90 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
class CircularQueue {
|
||||
constructor(size) {
|
||||
|
||||
this.queue = [];
|
||||
this.read = 0;
|
||||
this.write = 0;
|
||||
this.max = size - 1;
|
||||
|
||||
while (size > 0) {
|
||||
this.queue.push(null);
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
print() {
|
||||
return this.queue;
|
||||
}
|
||||
|
||||
enqueue(item) {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
dequeue() {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class CircularQueue {
|
||||
constructor(size) {
|
||||
this.queue = [];
|
||||
this.read = 0;
|
||||
this.write = 0;
|
||||
this.max = size - 1;
|
||||
|
||||
while (size > 0) {
|
||||
this.queue.push(null);
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
print() {
|
||||
return this.queue;
|
||||
}
|
||||
|
||||
enqueue(item) {
|
||||
// Only change code below this line
|
||||
console.log(this.write, this.max);
|
||||
if (this.queue[this.write] === null) {
|
||||
this.queue[this.write++] = item;
|
||||
|
||||
if (this.write > this.max) {
|
||||
this.write = 0;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
return null;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
dequeue() {
|
||||
// Only change code below this line
|
||||
if (this.queue[this.read] !== null) {
|
||||
let item = this.queue[this.read];
|
||||
this.queue[this.read++] = null;
|
||||
if (this.read > this.max) {
|
||||
this.read = 0;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
return null;
|
||||
// Only change code above this line
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825a367417b2b2512c87
|
||||
title: 创建双向链接列表
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-doubly-linked-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -147,5 +148,65 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
DoublyLinkedList.prototype = Object.assign(
|
||||
DoublyLinkedList.prototype,
|
||||
{
|
||||
|
||||
print() {
|
||||
if (this.head == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
var node = this.head;
|
||||
while (node.next != null) {
|
||||
result.push(node.data);
|
||||
node = node.next;
|
||||
};
|
||||
result.push(node.data);
|
||||
return result;
|
||||
};
|
||||
},
|
||||
printReverse() {
|
||||
if (this.tail == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
var node = this.tail;
|
||||
while (node.prev != null) {
|
||||
result.push(node.data);
|
||||
node = node.prev;
|
||||
};
|
||||
result.push(node.data);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var Node = function(data, prev) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = null;
|
||||
};
|
||||
var DoublyLinkedList = function() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825b367417b2b2512c8e
|
||||
title: 创建一个哈希表
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-hash-table
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -147,5 +148,90 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var called = 0;
|
||||
var hash = string => {
|
||||
called++;
|
||||
var hash = 0;
|
||||
for (var i = 0; i < string.length; i++) {
|
||||
hash += string.charCodeAt(i);
|
||||
}
|
||||
return hash;
|
||||
};
|
||||
|
||||
var addMethodSolution = function(key, val) {
|
||||
var theHash = hash(key);
|
||||
if (!this.collection.hasOwnProperty(theHash)) {
|
||||
this.collection[theHash] = {};
|
||||
}
|
||||
this.collection[theHash][key] = val;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var called = 0;
|
||||
var hash = string => {
|
||||
called++;
|
||||
var hashed = 0;
|
||||
for (var i = 0; i < string.length; i++) {
|
||||
hashed += string.charCodeAt(i);
|
||||
}
|
||||
return hashed;
|
||||
};
|
||||
var HashTable = function() {
|
||||
this.collection = {};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var called = 0;
|
||||
var hash = (string) => {
|
||||
called++;
|
||||
var hash = 0;
|
||||
for (var i = 0; i < string.length; i++) { hash += string.charCodeAt(i); }
|
||||
return hash;
|
||||
};
|
||||
var HashTable = function() {
|
||||
this.collection = {};
|
||||
// Only change code below this line
|
||||
|
||||
this.add = function(key, val) {
|
||||
var theHash = hash(key);
|
||||
if (!this.collection.hasOwnProperty(theHash)) {
|
||||
this.collection[theHash] = {};
|
||||
}
|
||||
this.collection[theHash][key] = val;
|
||||
}
|
||||
|
||||
this.remove = function(key) {
|
||||
var theHash = hash(key);
|
||||
var hashedObj = this.collection[theHash];
|
||||
if (hashedObj.hasOwnProperty(key)) {
|
||||
delete hashedObj[key];
|
||||
}
|
||||
if (!Object.keys(hashedObj).length) {
|
||||
delete this.collection[theHash];
|
||||
}
|
||||
}
|
||||
|
||||
this.lookup = function(key) {
|
||||
var theHash = hash(key);
|
||||
if (this.collection.hasOwnProperty(theHash)) {
|
||||
return this.collection[theHash][key];
|
||||
}
|
||||
return null
|
||||
}
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8251367417b2b2512c62
|
||||
title: 创建链接列表类
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-linked-list-class
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -64,5 +65,71 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
// Only change code below this line
|
||||
if (head == null) {
|
||||
head = new Node(element);
|
||||
}
|
||||
else {
|
||||
let currentNode = head;
|
||||
while (currentNode.next != null) {
|
||||
// currentNode.next will be last node of linked list after loop
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
currentNode.next = new Node(element);
|
||||
}
|
||||
length++;
|
||||
// Only change code above this line
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 8d5823c8c441eddfaeb5bdef
|
||||
title: 创建地图数据结构
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-map-data-structure
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -157,5 +158,55 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var Map = function() {
|
||||
this.collection = {};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var Map = function() {
|
||||
this.collection = {};
|
||||
// Only change code below this line
|
||||
|
||||
this.add = function(key,value) {
|
||||
this.collection[key] = value;
|
||||
}
|
||||
|
||||
this.remove = function(key) {
|
||||
delete this.collection[key];
|
||||
}
|
||||
|
||||
this.get = function(key) {
|
||||
return this.collection[key];
|
||||
}
|
||||
|
||||
this.has = function(key) {
|
||||
return this.collection.hasOwnProperty(key)
|
||||
}
|
||||
|
||||
this.values = function() {
|
||||
return Object.values(this.collection);
|
||||
}
|
||||
|
||||
this.size = function() {
|
||||
return Object.keys(this.collection).length;
|
||||
}
|
||||
|
||||
this.clear = function() {
|
||||
for(let item of Object.keys(this.collection)) {
|
||||
delete this.collection[item];
|
||||
}
|
||||
}
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8255367417b2b2512c74
|
||||
title: 创建优先级队列类
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-priority-queue-class
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -114,5 +115,63 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function PriorityQueue () {
|
||||
this.collection = [];
|
||||
this.printCollection = function() {
|
||||
console.log(this.collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function PriorityQueue() {
|
||||
this.collection = [];
|
||||
this.printCollection = function () {
|
||||
console.log(this.collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
this.enqueue = function (newitem) {
|
||||
if (this.isEmpty()) {
|
||||
return this.collection.push(newitem);
|
||||
}
|
||||
|
||||
this.collection = this.collection.reverse();
|
||||
var found_index = this.collection.findIndex(function (item) {
|
||||
return newitem[1] >= item[1];
|
||||
});
|
||||
if (found_index === -1) {
|
||||
this.collection.push(newitem);
|
||||
} else {
|
||||
this.collection.splice(found_index, 0, newitem);
|
||||
}
|
||||
this.collection = this.collection.reverse();
|
||||
};
|
||||
this.dequeue = function () {
|
||||
if (!this.isEmpty()) {
|
||||
return this.collection.shift()[0];
|
||||
} else {
|
||||
return "The queue is empty.";
|
||||
}
|
||||
};
|
||||
this.size = function () {
|
||||
return this.collection.length;
|
||||
};
|
||||
this.front = function () {
|
||||
return this.collection[0][0];
|
||||
};
|
||||
this.isEmpty = function () {
|
||||
return this.size() > 0 ? false : true;
|
||||
};
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8250367417b2b2512c60
|
||||
title: 创建队列类
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-queue-class
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -116,5 +117,50 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Queue() {
|
||||
var collection = [];
|
||||
this.print = function() {
|
||||
console.log(collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Queue () {
|
||||
var collection = [];
|
||||
this.print = function() {
|
||||
console.log(collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
this.enqueue = function(item) {
|
||||
collection.push(item);
|
||||
}
|
||||
|
||||
this.dequeue = function() {
|
||||
return collection.shift();
|
||||
}
|
||||
|
||||
this.front = function() {
|
||||
return collection[0];
|
||||
}
|
||||
|
||||
this.size = function(){
|
||||
return collection.length;
|
||||
}
|
||||
|
||||
this.isEmpty = function() {
|
||||
return collection.length === 0 ? true : false;
|
||||
}
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 8d1323c8c441eddfaeb5bdef
|
||||
title: 创建一个Set类
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-set-class
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,73 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
// Dictionary will hold the items of our set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
// This method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
// This method will return all the values in the set
|
||||
values() {
|
||||
return Object.values(this.dictionary);
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.values(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = element;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8250367417b2b2512c5f
|
||||
title: 创建一个堆栈类
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-stack-class
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -114,5 +115,46 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Stack() {
|
||||
var collection = [];
|
||||
this.print = function() {
|
||||
console.log(collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class Stack {
|
||||
constructor() {
|
||||
this.collection = [];
|
||||
}
|
||||
print() {
|
||||
console.log(this.collection);
|
||||
}
|
||||
push(val) {
|
||||
this.collection.push(val);
|
||||
}
|
||||
pop() {
|
||||
return this.collection.pop();
|
||||
}
|
||||
peek() {
|
||||
return this.collection[this.collection.length - 1];
|
||||
}
|
||||
isEmpty() {
|
||||
return this.collection.length === 0;
|
||||
}
|
||||
clear() {
|
||||
return (this.collection.length = 0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8259367417b2b2512c84
|
||||
title: 创建Trie搜索树
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-a-trie-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -116,5 +117,31 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
var Node = function() {
|
||||
this.keys = new Map();
|
||||
this.end = false;
|
||||
this.setEnd = function() {
|
||||
this.end = true;
|
||||
};
|
||||
this.isEnd = function() {
|
||||
return this.end;
|
||||
};
|
||||
};
|
||||
var Trie = function() {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825b367417b2b2512c8d
|
||||
title: 创建ES6 JavaScript地图
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-an-es6-javascript-map
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -23,5 +24,15 @@ myMap包含键值对`freeCodeCamp` , `Awesome!` 。
|
||||
assert(myMap.get('freeCodeCamp') === 'Awesome!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8254367417b2b2512c70
|
||||
title: 在ES6中创建和添加集
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: create-and-add-to-sets-in-es6
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -42,5 +43,26 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function checkSet() {
|
||||
var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
console.log(Array.from(set));
|
||||
return set;
|
||||
}
|
||||
|
||||
checkSet();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function checkSet(){var set = new Set([1,2,3,'Taco','Cat','Awesome']);
|
||||
return set;}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8258367417b2b2512c80
|
||||
title: 删除二进制搜索树中的叶节点
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: delete-a-leaf-node-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -109,5 +110,82 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return searchTree(node);
|
||||
}
|
||||
},
|
||||
inorder: function() {
|
||||
if (this.root == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
function traverseInOrder(node) {
|
||||
if (node.left != null) {
|
||||
traverseInOrder(node.left);
|
||||
}
|
||||
result.push(node.value);
|
||||
if (node.right != null) {
|
||||
traverseInOrder(node.right);
|
||||
}
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8258367417b2b2512c81
|
||||
title: 在二叉搜索树中删除具有一个子节点的节点
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: delete-a-node-with-one-child-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -157,5 +158,124 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return searchTree(node);
|
||||
}
|
||||
},
|
||||
inorder: function() {
|
||||
if (this.root == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
function traverseInOrder(node) {
|
||||
if (node.left != null) {
|
||||
traverseInOrder(node.left);
|
||||
}
|
||||
result.push(node.value);
|
||||
if (node.right != null) {
|
||||
traverseInOrder(node.right);
|
||||
}
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.remove = function(value) {
|
||||
if (this.root === null) {
|
||||
return null;
|
||||
}
|
||||
var target;
|
||||
var parent = null;
|
||||
// Find the target value and its parent
|
||||
(function findValue(node = this.root) {
|
||||
if (value == node.value) {
|
||||
target = node;
|
||||
} else if (value < node.value && node.left !== null) {
|
||||
parent = node;
|
||||
return findValue(node.left);
|
||||
} else if (value < node.value && node.left === null) {
|
||||
return null;
|
||||
} else if (value > node.value && node.right !== null) {
|
||||
parent = node;
|
||||
return findValue(node.right);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}.bind(this)());
|
||||
if (target === null) {
|
||||
return null;
|
||||
}
|
||||
// Count the children of the target to delete
|
||||
var children =
|
||||
(target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
|
||||
// Case 1: Target has no children
|
||||
if (children === 0) {
|
||||
if (target == this.root) {
|
||||
this.root = null;
|
||||
} else {
|
||||
if (parent.left == target) {
|
||||
parent.left = null;
|
||||
} else {
|
||||
parent.right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Case 2: Target has one child
|
||||
// Only change code below this line
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8258367417b2b2512c82
|
||||
title: 在二叉搜索树中删除具有两个子节点的节点
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: delete-a-node-with-two-children-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -230,5 +231,165 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return searchTree(node);
|
||||
}
|
||||
},
|
||||
inorder: function() {
|
||||
if (this.root == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
function traverseInOrder(node) {
|
||||
if (node.left != null) {
|
||||
traverseInOrder(node.left);
|
||||
}
|
||||
result.push(node.value);
|
||||
if (node.right != null) {
|
||||
traverseInOrder(node.right);
|
||||
}
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
}
|
||||
},
|
||||
isBinarySearchTree() {
|
||||
if (this.root == null) {
|
||||
return null;
|
||||
} else {
|
||||
var check = true;
|
||||
function checkTree(node) {
|
||||
if (node.left != null) {
|
||||
var left = node.left;
|
||||
if (left.value > node.value) {
|
||||
check = false;
|
||||
} else {
|
||||
checkTree(left);
|
||||
}
|
||||
}
|
||||
if (node.right != null) {
|
||||
var right = node.right;
|
||||
if (right.value < node.value) {
|
||||
check = false;
|
||||
} else {
|
||||
checkTree(right);
|
||||
}
|
||||
}
|
||||
}
|
||||
checkTree(this.root);
|
||||
return check;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.remove = function(value) {
|
||||
if (this.root === null) {
|
||||
return null;
|
||||
}
|
||||
var target;
|
||||
var parent = null;
|
||||
// Find the target value and its parent
|
||||
(function findValue(node = this.root) {
|
||||
if (value == node.value) {
|
||||
target = node;
|
||||
} else if (value < node.value && node.left !== null) {
|
||||
parent = node;
|
||||
return findValue(node.left);
|
||||
} else if (value < node.value && node.left === null) {
|
||||
return null;
|
||||
} else if (value > node.value && node.right !== null) {
|
||||
parent = node;
|
||||
return findValue(node.right);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}.bind(this)());
|
||||
if (target === null) {
|
||||
return null;
|
||||
}
|
||||
// Count the children of the target to delete
|
||||
var children =
|
||||
(target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
|
||||
// Case 1: Target has no children
|
||||
if (children === 0) {
|
||||
if (target == this.root) {
|
||||
this.root = null;
|
||||
} else {
|
||||
if (parent.left == target) {
|
||||
parent.left = null;
|
||||
} else {
|
||||
parent.right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Case 2: Target has one child
|
||||
else if (children == 1) {
|
||||
var newChild = target.left !== null ? target.left : target.right;
|
||||
if (parent === null) {
|
||||
target.value = newChild.value;
|
||||
target.left = null;
|
||||
target.right = null;
|
||||
} else if (newChild.value < parent.value) {
|
||||
parent.left = newChild;
|
||||
} else {
|
||||
parent.right = newChild;
|
||||
}
|
||||
target = null;
|
||||
}
|
||||
// Case 3: Target has two children
|
||||
// Only change code below this line
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825d367417b2b2512c96
|
||||
title: 深度优先搜索
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: depth-first-search
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -151,5 +152,45 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function dfs(graph, root) {
|
||||
|
||||
}
|
||||
|
||||
var exDFSGraph = [
|
||||
[0, 1, 0, 0],
|
||||
[1, 0, 1, 0],
|
||||
[0, 1, 0, 1],
|
||||
[0, 0, 1, 0]
|
||||
];
|
||||
console.log(dfs(exDFSGraph, 3));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function dfs(graph, root) {
|
||||
var stack = [];
|
||||
var tempV;
|
||||
var visited = [];
|
||||
var tempVNeighbors = [];
|
||||
stack.push(root);
|
||||
while (stack.length > 0) {
|
||||
tempV = stack.pop();
|
||||
if (visited.indexOf(tempV) == -1) {
|
||||
visited.push(tempV);
|
||||
tempVNeighbors = graph[tempV];
|
||||
for (var i = 0; i < tempVNeighbors.length; i++) {
|
||||
if (tempVNeighbors[i] == 1) {
|
||||
stack.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return visited;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8257367417b2b2512c7d
|
||||
title: 找到二叉搜索树的最小和最大高度
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: find-the-minimum-and-maximum-height-of-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -173,5 +174,139 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
return searchTree(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
this.findMinHeight = function(root = this.root) {
|
||||
// empty tree.
|
||||
if (root === null) {
|
||||
return -1;
|
||||
}
|
||||
// leaf node.
|
||||
if (root.left === null && root.right === null) {
|
||||
return 0;
|
||||
}
|
||||
if (root.left === null) {
|
||||
return this.findMinHeight(root.right) + 1;
|
||||
}
|
||||
if (root.right === null) {
|
||||
return this.findMinHeight(root.left) + 1;
|
||||
}
|
||||
const lHeight = this.findMinHeight(root.left);
|
||||
const rHeight = this.findMinHeight(root.right);
|
||||
return Math.min(lHeight, rHeight) + 1;
|
||||
};
|
||||
this.findMaxHeight = function(root = this.root) {
|
||||
// empty tree.
|
||||
if (root === null) {
|
||||
return -1;
|
||||
}
|
||||
// leaf node.
|
||||
if (root.left === null && root.right === null) {
|
||||
return 0;
|
||||
}
|
||||
if (root.left === null) {
|
||||
return this.findMaxHeight(root.right) + 1;
|
||||
}
|
||||
if (root.right === null) {
|
||||
return this.findMaxHeight(root.left) + 1;
|
||||
}
|
||||
const lHeight = this.findMaxHeight(root.left);
|
||||
const rHeight = this.findMaxHeight(root.right);
|
||||
return Math.max(lHeight, rHeight) + 1;
|
||||
};
|
||||
this.isBalanced = function(root = this.root) {
|
||||
if (root === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (root.left === null && root.right === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (root.left === null) {
|
||||
return this.findMaxHeight(root.right) <= 0;
|
||||
}
|
||||
|
||||
if (root.right === null) {
|
||||
return this.findMaxHeight(root.left) <= 0;
|
||||
}
|
||||
|
||||
const lHeight = this.findMaxHeight(root.left);
|
||||
const rHeight = this.findMaxHeight(root.right);
|
||||
if (Math.abs(lHeight - rHeight) > 1) {
|
||||
return false;
|
||||
}
|
||||
return this.isBalanced(root.left) && this.isBalanced(root.right);
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8256367417b2b2512c7a
|
||||
title: 在二叉搜索树中查找最小值和最大值
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: find-the-minimum-and-maximum-value-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -135,5 +136,236 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
return searchTree(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.findMin = function() {
|
||||
// Empty tree.
|
||||
if (!this.root) {
|
||||
return null;
|
||||
}
|
||||
let currentNode = this.root;
|
||||
while (currentNode.left) {
|
||||
currentNode = currentNode.left;
|
||||
}
|
||||
return currentNode.value;
|
||||
};
|
||||
this.findMax = function() {
|
||||
// Empty tree.
|
||||
if (!this.root) {
|
||||
return null;
|
||||
}
|
||||
let currentNode = this.root;
|
||||
while (currentNode.right) {
|
||||
currentNode = currentNode.right;
|
||||
}
|
||||
return currentNode.value;
|
||||
};
|
||||
this.add = function(value) {
|
||||
// Empty tree.
|
||||
if (!this.root) {
|
||||
this.root = new Node(value);
|
||||
return undefined;
|
||||
}
|
||||
return this.addNode(this.root, value);
|
||||
};
|
||||
this.addNode = function(node, value) {
|
||||
// Check if value already exists.
|
||||
if (node.value === value) return null;
|
||||
if (value < node.value) {
|
||||
if (node.left) {
|
||||
return this.addNode(node.left, value);
|
||||
} else {
|
||||
node.left = new Node(value);
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
if (node.right) {
|
||||
return this.addNode(node.right, value);
|
||||
} else {
|
||||
node.right = new Node(value);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
};
|
||||
this.isPresent = function(value) {
|
||||
if (!this.root) {
|
||||
return null;
|
||||
}
|
||||
return this.isNodePresent(this.root, value);
|
||||
};
|
||||
this.isNodePresent = function(node, value) {
|
||||
if (node.value === value) return true;
|
||||
if (value < node.value) {
|
||||
return node.left ? this.isNodePresent(node.left, value) : false;
|
||||
} else {
|
||||
return node.right ? this.isNodePresent(node.right, value) : false;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
this.findMinHeight = function() {
|
||||
if (!this.root) {
|
||||
return -1;
|
||||
}
|
||||
let heights = {};
|
||||
let height = 0;
|
||||
this.traverseTree(this.root, height, heights);
|
||||
return Math.min(...Object.keys(heights));
|
||||
};
|
||||
this.findMaxHeight = function() {
|
||||
if (!this.root) {
|
||||
return -1;
|
||||
}
|
||||
let heights = {};
|
||||
let height = 0;
|
||||
this.traverseTree(this.root, height, heights);
|
||||
return Math.max(...Object.keys(heights));
|
||||
};
|
||||
this.traverseTree = function(node, height, heights) {
|
||||
if (node.left === null && node.right === null) {
|
||||
return (heights[height] = true);
|
||||
}
|
||||
if (node.left) {
|
||||
this.traverseTree(node.left, height + 1, heights);
|
||||
}
|
||||
if (node.right) {
|
||||
this.traverseTree(node.right, height + 1, heights);
|
||||
}
|
||||
};
|
||||
this.isBalanced = function() {
|
||||
return this.findMaxHeight() > this.findMinHeight() + 1;
|
||||
};
|
||||
// DFS tree traversal.
|
||||
this.inorder = function() {
|
||||
if (!this.root) return null;
|
||||
let result = [];
|
||||
|
||||
function traverseInOrder(node) {
|
||||
if (node.left) traverseInOrder(node.left);
|
||||
result.push(node.value);
|
||||
if (node.right) traverseInOrder(node.right);
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
};
|
||||
this.preorder = function() {
|
||||
if (!this.root) return null;
|
||||
let result = [];
|
||||
|
||||
function traverseInOrder(node) {
|
||||
result.push(node.value);
|
||||
if (node.left) traverseInOrder(node.left);
|
||||
if (node.right) traverseInOrder(node.right);
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
};
|
||||
this.postorder = function() {
|
||||
if (!this.root) return null;
|
||||
let result = [];
|
||||
|
||||
function traverseInOrder(node) {
|
||||
if (node.left) traverseInOrder(node.left);
|
||||
if (node.right) traverseInOrder(node.right);
|
||||
result.push(node.value);
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
};
|
||||
// BFS tree traversal.
|
||||
this.levelOrder = function() {
|
||||
if (!this.root) return null;
|
||||
let queue = [this.root];
|
||||
let result = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift();
|
||||
result.push(node.value);
|
||||
if (node.left) queue.push(node.left);
|
||||
if (node.right) queue.push(node.right);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
this.reverseLevelOrder = function() {
|
||||
if (!this.root) return null;
|
||||
let queue = [this.root];
|
||||
let result = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift();
|
||||
result.push(node.value);
|
||||
if (node.right) queue.push(node.right);
|
||||
if (node.left) queue.push(node.left);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
// Delete a leaf node.
|
||||
}
|
||||
let bst = new BinarySearchTree();
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825b367417b2b2512c8c
|
||||
title: 用最小堆实现堆排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-heap-sort-with-a-min-heap
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -99,5 +100,36 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
// Generate a randomly filled array
|
||||
function createRandomArray(size = 5){
|
||||
let a = new Array(size);
|
||||
for(let i = 0; i < size; i++)
|
||||
a[i] = Math.floor(Math.random() * 100);
|
||||
|
||||
return a;
|
||||
}
|
||||
const array = createRandomArray(25);
|
||||
|
||||
var MinHeap = function() {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8256367417b2b2512c79
|
||||
title: 发生率矩阵
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: incidence-matrix
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -101,5 +102,18 @@ assert(incMatUndirected[2][2] === 1 && incMatUndirected[4][2] === 1);
|
||||
assert(incMatUndirected[1][3] === 1 && incMatUndirected[3][3] === 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var incMatUndirected = [
|
||||
|
||||
];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var incMatUndirected = [[1, 0, 0, 0],[1, 1, 0, 1],[0, 1, 1, 0],[0, 0, 0, 1],[0, 0, 1, 0]];
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825a367417b2b2512c8a
|
||||
title: 将元素插入最大堆
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: insert-an-element-into-a-max-heap
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,38 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
// Only change code below this line
|
||||
this.heap = [null];
|
||||
this.insert = (ele) => {
|
||||
var index = this.heap.length;
|
||||
var arr = [...this.heap];
|
||||
arr.push(ele);
|
||||
while (ele > arr[Math.floor(index / 2)] && index > 1) {
|
||||
arr[index] = arr[Math.floor(index / 2)];
|
||||
arr[Math.floor(index / 2)] = ele;
|
||||
index = arr[Math.floor(index / 2)];
|
||||
}
|
||||
this.heap = arr;
|
||||
}
|
||||
this.print = () => {
|
||||
return this.heap.slice(1);
|
||||
}
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8259367417b2b2512c83
|
||||
title: 反转二叉树
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: invert-a-binary-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -88,5 +89,104 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left)
|
||||
};
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
return searchTree(node);
|
||||
};
|
||||
},
|
||||
inorder: function() {
|
||||
if (this.root == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
function traverseInOrder(node) {
|
||||
if (node.left != null) {
|
||||
traverseInOrder(node.left);
|
||||
};
|
||||
result.push(node.value);
|
||||
if (node.right != null) {
|
||||
traverseInOrder(node.right);
|
||||
};
|
||||
}
|
||||
traverseInOrder(this.root);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
this.invert = function(node = this.root) {
|
||||
if (node) {
|
||||
const temp = node.left;
|
||||
node.left = node.right;
|
||||
node.right = temp;
|
||||
this.invert(node.left);
|
||||
this.invert(node.right);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8250367417b2b2512c5e
|
||||
title: 了解堆栈的工作原理
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: learn-how-a-stack-works
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -44,5 +45,17 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var homeworkStack = ["BIO12","HIS80","MAT122","PSY44"];
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8254367417b2b2512c6e
|
||||
title: 对两组数据执行差异
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: perform-a-difference-on-two-sets-of-data
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -43,5 +44,174 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// This method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// This method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// This method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// This is our union method
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// This is our intersection method
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
difference(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
this.values().forEach(value => {
|
||||
if (!set.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8254367417b2b2512c6f
|
||||
title: 对两组数据执行子集检查
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: perform-a-subset-check-on-two-sets-of-data
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -107,5 +108,193 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// This method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// This method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// This method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// This is our union method
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// This is our intersection method
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
difference(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
this.values().forEach(value => {
|
||||
if (!set.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
difference(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
this.values().forEach(value => {
|
||||
if (!set.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
isSubsetOf(set) {
|
||||
for(const value of this.values()){
|
||||
if(!set.dictionary[value]) return false;
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8253367417b2b2512c6c
|
||||
title: 在两个集上执行联合
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: perform-a-union-on-two-sets
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -47,5 +48,106 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// This method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// This method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// This method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8253367417b2b2512c6d
|
||||
title: 在两组数据上执行交集
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: perform-an-intersection-on-two-sets-of-data
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -42,5 +43,140 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// This method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// This method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// This method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// This method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// This is our union method
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825b367417b2b2512c8b
|
||||
title: 从最大堆中删除元素
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: remove-an-element-from-a-max-heap
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -98,5 +99,35 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var MaxHeap = function() {
|
||||
this.heap = [null];
|
||||
this.insert = (ele) => {
|
||||
var index = this.heap.length;
|
||||
var arr = [...this.heap];
|
||||
arr.push(ele);
|
||||
while (ele > arr[Math.floor(index / 2)] && index > 1) {
|
||||
arr[index] = arr[Math.floor(index / 2)];
|
||||
arr[Math.floor(index / 2)] = ele;
|
||||
index = arr[Math.floor(index / 2)];
|
||||
}
|
||||
this.heap = arr;
|
||||
}
|
||||
this.print = () => {
|
||||
return this.heap.slice(1);
|
||||
}
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8251367417b2b2512c65
|
||||
title: 按索引从链接列表中删除元素
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: remove-elements-from-a-linked-list-by-index
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -88,5 +89,109 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function (element) {
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function () {
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function () {
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function (element) {
|
||||
var node = new Node(element);
|
||||
if (head === null) {
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while (currentNode.next) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.removeAt = function (index) {
|
||||
var currentNode = head;
|
||||
var previous = head;
|
||||
var count = 0;
|
||||
if (index >= length || index < 0) {
|
||||
return null;
|
||||
}
|
||||
if (index === 0) {
|
||||
var removed = head.element;
|
||||
head = currentNode.next;
|
||||
} else {
|
||||
while (count < index) {
|
||||
previous = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
count++;
|
||||
}
|
||||
var removed = previous.next.element;
|
||||
previous.next = currentNode.next;
|
||||
}
|
||||
length--;
|
||||
return removed;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8251367417b2b2512c63
|
||||
title: 从链接列表中删除元素
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: remove-elements-from-a-linked-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -73,5 +74,112 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function(){
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
if (head === null) {
|
||||
return;
|
||||
}
|
||||
var previous;
|
||||
var currentNode = head;
|
||||
|
||||
while (currentNode.next !== null && currentNode.element !== element) {
|
||||
previous = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
if (currentNode.next === null && currentNode.element !== element) {
|
||||
return;
|
||||
}
|
||||
else if (previous) {
|
||||
previous.next = currentNode.next;
|
||||
} else {
|
||||
head = currentNode.next;
|
||||
}
|
||||
|
||||
length--;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8254367417b2b2512c71
|
||||
title: 从ES6中的集中删除项目
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: remove-items-from-a-set-in-es6
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -31,5 +32,23 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function checkSet(){
|
||||
var set = null;
|
||||
return set;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function checkSet(){
|
||||
var set = new Set([1,2,3,4,5]);
|
||||
set.delete(2);
|
||||
set.delete(5);
|
||||
return set;}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825a367417b2b2512c88
|
||||
title: 反转双重链接列表
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: reverse-a-doubly-linked-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -109,5 +110,109 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
DoublyLinkedList.prototype = Object.assign(
|
||||
DoublyLinkedList.prototype,
|
||||
{
|
||||
add(data) {
|
||||
if (this.head == null) {
|
||||
this.head = new Node(data, null);
|
||||
this.tail = this.head;
|
||||
} else {
|
||||
var node = this.head;
|
||||
var prev = null;
|
||||
while (node.next != null) {
|
||||
prev = node;
|
||||
node = node.next;
|
||||
};
|
||||
var newNode = new Node(data, node);
|
||||
node.next = newNode;
|
||||
this.tail = newNode;
|
||||
};
|
||||
},
|
||||
print() {
|
||||
if (this.head == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
var node = this.head;
|
||||
while (node.next != null) {
|
||||
result.push(node.data);
|
||||
node = node.next;
|
||||
};
|
||||
result.push(node.data);
|
||||
return result;
|
||||
};
|
||||
},
|
||||
printReverse() {
|
||||
if (this.tail == null) {
|
||||
return null;
|
||||
} else {
|
||||
var result = new Array();
|
||||
var node = this.tail;
|
||||
while (node.prev != null) {
|
||||
result.push(node.data);
|
||||
node = node.prev;
|
||||
};
|
||||
result.push(node.data);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var Node = function(data, prev) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = null;
|
||||
};
|
||||
var DoublyLinkedList = function() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var Node = function(data, prev) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = null;
|
||||
};
|
||||
var DoublyLinkedList = function() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
|
||||
this.reverse = function() {
|
||||
if (!this.head || !this.head.next) {
|
||||
return this.head
|
||||
}
|
||||
|
||||
let tail;
|
||||
let temp;
|
||||
let current = this.head;
|
||||
while(current !== null) {
|
||||
if(!tail) tail = current;
|
||||
temp = current.prev;
|
||||
current.prev = current.next;
|
||||
current.next = temp;
|
||||
current = current.prev;
|
||||
}
|
||||
|
||||
this.head = temp.prev;
|
||||
this.tail = tail
|
||||
}
|
||||
};
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8251367417b2b2512c64
|
||||
title: 在链接列表中搜索
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: search-within-a-linked-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,160 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function() {
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
var currentNode = head;
|
||||
var previousNode;
|
||||
if(currentNode.element === element){
|
||||
head = currentNode.next;
|
||||
} else {
|
||||
while(currentNode.element !== element) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
previousNode.next = currentNode.next;
|
||||
}
|
||||
|
||||
length --;
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function LinkedList() {
|
||||
var length = 0;
|
||||
var head = null;
|
||||
|
||||
var Node = function(element){
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
|
||||
this.size = function() {
|
||||
return length;
|
||||
};
|
||||
|
||||
this.head = function(){
|
||||
return head;
|
||||
};
|
||||
|
||||
this.add = function(element){
|
||||
var node = new Node(element);
|
||||
if(head === null){
|
||||
head = node;
|
||||
} else {
|
||||
var currentNode = head;
|
||||
|
||||
while(currentNode.next){
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
currentNode.next = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
};
|
||||
|
||||
this.remove = function(element){
|
||||
var currentNode = head;
|
||||
var previousNode;
|
||||
if(currentNode.element === element){
|
||||
head = currentNode.next;
|
||||
} else {
|
||||
while(currentNode.element !== element) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
previousNode.next = currentNode.next;
|
||||
}
|
||||
|
||||
length --;
|
||||
};
|
||||
|
||||
this.indexOf = function(element) {
|
||||
if (head === null) return -1
|
||||
|
||||
let current = head;
|
||||
let index = 0;
|
||||
|
||||
while (current.element !== element && current.next !== null) {
|
||||
current = current.next;
|
||||
index++
|
||||
}
|
||||
|
||||
if (current.element !== element && current.next === null) {
|
||||
return -1
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
this.elementAt = function(index) {
|
||||
if (head === null) return undefined;
|
||||
|
||||
let current = head;
|
||||
let currentIndex = 0;
|
||||
|
||||
while (currentIndex !== index && current.next !== null) {
|
||||
current = current.next;
|
||||
currentIndex++
|
||||
}
|
||||
|
||||
if (currentIndex !== index && current.next === null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return current.element;
|
||||
}
|
||||
|
||||
this.isEmpty = function() {
|
||||
return length === 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8253367417b2b2512c6a
|
||||
title: 键入的数组
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: typed-arrays
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -80,5 +81,18 @@ assert(i32View.byteLength === 64);
|
||||
assert(i32View.length === 16);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var buffer;
|
||||
var i32View;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var buffer = new ArrayBuffer(64);
|
||||
var i32View = new Int32Array(buffer);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8255367417b2b2512c72
|
||||
title: 在ES6集上使用.has和.size
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: use--has-and--size-on-an-es6-set
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -26,5 +27,29 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function checkSet(arrToBeSet, checkValue){
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function checkSet(arrToBeSet, checkValue){
|
||||
var set = new Set(arrToBeSet);
|
||||
var result = [
|
||||
set.has(checkValue),
|
||||
set.size
|
||||
];
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8258367417b2b2512c7f
|
||||
title: 在二叉搜索树中使用广度优先搜索
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: use-breadth-first-search-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -155,5 +156,100 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
return searchTree(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
this.levelOrder = (root = this.root) => {
|
||||
if(!root) return null;
|
||||
let queue = [root];
|
||||
let results = [];
|
||||
while(queue.length > 0) {
|
||||
let node = queue.shift();
|
||||
results.push(node.value);
|
||||
if(node.left) queue.push(node.left);
|
||||
if(node.right) queue.push(node.right);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
this.reverseLevelOrder = (root = this.root) => {
|
||||
if(!root) return null;
|
||||
let queue = [root];
|
||||
let results = [] ;
|
||||
while ( queue.length > 0) {
|
||||
let node = queue.shift();
|
||||
results.push(node.value);
|
||||
if(node.right) queue.push(node.right);
|
||||
if(node.left ) queue.push(node.left);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8257367417b2b2512c7e
|
||||
title: 在二叉搜索树中使用深度优先搜索
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: use-depth-first-search-in-a-binary-search-tree
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -220,5 +221,104 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
BinarySearchTree.prototype = Object.assign(
|
||||
BinarySearchTree.prototype,
|
||||
{
|
||||
add: function(value) {
|
||||
function searchTree(node) {
|
||||
if (value < node.value) {
|
||||
if (node.left == null) {
|
||||
node.left = new Node(value);
|
||||
return;
|
||||
} else if (node.left != null) {
|
||||
return searchTree(node.left);
|
||||
}
|
||||
} else if (value > node.value) {
|
||||
if (node.right == null) {
|
||||
node.right = new Node(value);
|
||||
return;
|
||||
} else if (node.right != null) {
|
||||
return searchTree(node.right);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var node = this.root;
|
||||
if (node == null) {
|
||||
this.root = new Node(value);
|
||||
return;
|
||||
} else {
|
||||
return searchTree(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
||||
function Node(value) {
|
||||
this.value = value;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
function BinarySearchTree() {
|
||||
this.root = null;
|
||||
this.result = [];
|
||||
|
||||
this.inorder = function(node) {
|
||||
if (!node) node = this.root;
|
||||
if (!node) return null;
|
||||
|
||||
if (node.left) this.inorder(node.left);
|
||||
this.result.push(node.value);
|
||||
if (node.right) this.inorder(node.right);
|
||||
return this.result;
|
||||
};
|
||||
this.preorder = function(node) {
|
||||
if (!node) node = this.root;
|
||||
if (!node) return null;
|
||||
|
||||
this.result.push(node.value);
|
||||
if (node.left) this.preorder(node.left);
|
||||
if (node.right) this.preorder(node.right);
|
||||
return this.result;
|
||||
};
|
||||
this.postorder = function(node) {
|
||||
if (!node) node = this.root;
|
||||
if (!node) return null;
|
||||
|
||||
if (node.left) this.postorder(node.left);
|
||||
if (node.right) this.postorder(node.right);
|
||||
this.result.push(node.value);
|
||||
|
||||
return this.result;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8255367417b2b2512c73
|
||||
title: 使用Spread和Notes进行ES5 Set()集成
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: use-spread-and-notes-for-es5-set-integration
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -30,5 +31,21 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function checkSet(set){
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function checkSet(set){
|
||||
return [...set];}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8251367417b2b2512c61
|
||||
title: 使用链接列表中的节点
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: work-with-nodes-in-a-linked-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -27,5 +28,24 @@ assert(Puppy.next.element === 'Cat');
|
||||
assert(Cat.next.element === 'Dog');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var Node = function(element) {
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
};
|
||||
var Kitten = new Node('Kitten');
|
||||
var Puppy = new Node('Puppy');
|
||||
|
||||
Kitten.next = Puppy;
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
Reference in New Issue
Block a user