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: a3f503de51cf954ede28891d
|
||||
title: 找到对称差异
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: find-the-symmetric-difference
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -115,5 +116,29 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sym(args) {
|
||||
return args;
|
||||
}
|
||||
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sym() {
|
||||
var arrays = [].slice.call(arguments);
|
||||
return arrays.reduce(function (symDiff, arr) {
|
||||
return symDiff.concat(arr).filter(function (val, idx, theArr) {
|
||||
return theArr.indexOf(val) === idx
|
||||
&& (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);
|
||||
});
|
||||
});
|
||||
}
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 8d5123c8c441eddfaeb5bdef
|
||||
title: 实施冒泡排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-bubble-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,56 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
bubbleSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
let swapped = false;
|
||||
for (let j = 1; j < array.length; j++) {
|
||||
if (array[j - 1] > array[j]) {
|
||||
let temp = array[j-1];
|
||||
array[j-1] = array[j];
|
||||
array[j] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (swapped === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8259367417b2b2512c86
|
||||
title: 实现插入排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-insertion-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,51 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
insertionSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function insertionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function insertionSort (array) {
|
||||
for (let currentIndex = 0; currentIndex < array.length; currentIndex++) {
|
||||
let current = array[currentIndex];
|
||||
let j = currentIndex - 1;
|
||||
while (j > -1 && array[j] > current) {
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j + 1] = current;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825c367417b2b2512c8f
|
||||
title: 实现合并排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-merge-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,70 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
mergeSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length === 1) {
|
||||
return array;
|
||||
} else {
|
||||
const splitIndex = Math.floor(array.length / 2);
|
||||
return merge(
|
||||
mergeSort(array.slice(0, splitIndex)),
|
||||
mergeSort(array.slice(splitIndex))
|
||||
);
|
||||
}
|
||||
|
||||
// Merge two sorted arrays
|
||||
function merge(array1, array2) {
|
||||
let merged = [];
|
||||
while (array1.length && array2.length) {
|
||||
if (array1[0] < array2[0]) {
|
||||
merged.push(array1.shift());
|
||||
} else if (array1[0] > array2[0]) {
|
||||
merged.push(array2.shift());
|
||||
} else {
|
||||
merged.push(array1.shift(), array2.shift());
|
||||
}
|
||||
}
|
||||
|
||||
// After looping ends, one array is empty, and other array contains only
|
||||
// values greater than all values in `merged`
|
||||
return [...merged, ...array1, ...array2];
|
||||
}
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825a367417b2b2512c89
|
||||
title: 实施快速排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-quick-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,60 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
quickSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
if (array.length === 0) {
|
||||
return [];
|
||||
} else {
|
||||
const pivotValue = array[0];
|
||||
|
||||
// Sort elements into three piles
|
||||
let lesser = [];
|
||||
let equal = [];
|
||||
let greater = [];
|
||||
for (let e of array) {
|
||||
if (e < pivotValue) {
|
||||
lesser.push(e);
|
||||
} else if (e > pivotValue) {
|
||||
greater.push(e);
|
||||
} else {
|
||||
equal.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
return [...quickSort(lesser), ...equal, ...quickSort(greater)];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8259367417b2b2512c85
|
||||
title: 实施选择排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-selection-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,54 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
selectionSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
|
||||
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
for (let i = 0; i < array.length-1; i++) {
|
||||
let minimumIndex = i;
|
||||
for (let j = i+1; j < array.length; j++){
|
||||
if (array[j] < array[minimumIndex]) {
|
||||
minimumIndex = j;
|
||||
}
|
||||
}
|
||||
let value = array[minimumIndex];
|
||||
array[minimumIndex] = array[i];
|
||||
array[i] = value;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: a56138aff60341a09ed6c480
|
||||
title: 库存更新
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: inventory-update
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -156,5 +157,72 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
return arr1;
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, "Bowling Ball"],
|
||||
[2, "Dirty Sock"],
|
||||
[1, "Hair Pin"],
|
||||
[5, "Microphone"]
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, "Hair Pin"],
|
||||
[3, "Half-Eaten Apple"],
|
||||
[67, "Bowling Ball"],
|
||||
[7, "Toothpaste"]
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
arr2.forEach(function(item) {
|
||||
createOrUpdate(arr1, item);
|
||||
});
|
||||
// All inventory must be accounted for or you're fired!
|
||||
return arr1;
|
||||
}
|
||||
|
||||
function createOrUpdate(arr1, item) {
|
||||
var index = -1;
|
||||
while (++index < arr1.length) {
|
||||
if (arr1[index][1] === item[1]) {
|
||||
arr1[index][0] += item[0];
|
||||
return;
|
||||
}
|
||||
if (arr1[index][1] > item[1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
arr1.splice(index, 0, item);
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: a7bf700cd123b9a54eef01d5
|
||||
title: 请不要重复
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: no-repeats-please
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -71,5 +72,53 @@ assert.strictEqual(permAlone('aaab'), 0);
|
||||
assert.strictEqual(permAlone('aaabb'), 12);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return permuter(str).filter(function(perm) {
|
||||
return !perm.match(/(.)\1/g);
|
||||
}).length;
|
||||
}
|
||||
|
||||
function permuter(str) {
|
||||
// http://staff.roguecc.edu/JMiller/JavaScript/permute.html
|
||||
//permArr: Global array which holds the list of permutations
|
||||
//usedChars: Global utility array which holds a list of "currently-in-use" characters
|
||||
var permArr = [], usedChars = [];
|
||||
function permute(input) {
|
||||
//convert input into a char array (one element for each character)
|
||||
var i, ch, chars = input.split("");
|
||||
for (i = 0; i < chars.length; i++) {
|
||||
//get and remove character at index "i" from char array
|
||||
ch = chars.splice(i, 1);
|
||||
//add removed character to the end of used characters
|
||||
usedChars.push(ch);
|
||||
//when there are no more characters left in char array to add, add used chars to list of permutations
|
||||
if (chars.length === 0) permArr[permArr.length] = usedChars.join("");
|
||||
//send characters (minus the removed one from above) from char array to be permuted
|
||||
permute(chars.join(""));
|
||||
//add removed character back into char array in original position
|
||||
chars.splice(i, 0, ch);
|
||||
//remove the last character used off the end of used characters array
|
||||
usedChars.pop();
|
||||
}
|
||||
}
|
||||
permute(str);
|
||||
return permArr;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: a3f503de51cfab748ff001aa
|
||||
title: 成对
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: pairwise
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -49,5 +50,37 @@ assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
assert.deepEqual(pairwise([], 100), 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
var sum = 0;
|
||||
arr.forEach(function(e, i, a) {
|
||||
if (e != null) {
|
||||
var diff = arg-e;
|
||||
a[i] = null;
|
||||
var dix = a.indexOf(diff);
|
||||
if (dix !== -1) {
|
||||
sum += dix;
|
||||
sum += i;
|
||||
a[dix] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
return sum;
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
```
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f36e1000cf542c50fe80
|
||||
title: 问题1:3和5的倍数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-1-multiples-of-3-and-5
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -37,5 +38,30 @@ assert.strictEqual(multiplesOf3and5(19564), 89301183);
|
||||
assert.strictEqual(multiplesOf3and5(8456), 16687353);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function multiplesOf3and5(number) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
multiplesOf3and5(1000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const multiplesOf3and5 = (number) => {
|
||||
var total = 0;
|
||||
|
||||
for(var i = 0; i < number; i++) {
|
||||
if(i % 3 == 0 || i % 5 == 0) {
|
||||
total += i;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
};
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3761000cf542c50fe89
|
||||
title: 问题10:素数的总和
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-10-summation-of-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -35,5 +36,38 @@ assert.strictEqual(primeSummation(140759), 873608362);
|
||||
assert.strictEqual(primeSummation(2000000), 142913828922);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primeSummation(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primeSummation(2000000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function primeSummation(n) {
|
||||
if (n < 3) { return 0 };
|
||||
let nums = [0, 0, 2];
|
||||
for (let i = 3; i < n; i += 2){
|
||||
nums.push(i);
|
||||
nums.push(0);
|
||||
}
|
||||
let sum = 2;
|
||||
for (let i = 3; i < n; i += 2){
|
||||
if (nums[i] !== 0){
|
||||
sum += nums[i];
|
||||
for (let j = i*i; j < n; j += i){
|
||||
nums[j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d01000cf542c50fee3
|
||||
title: 问题100:安排概率
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-100-arranged-probability
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler100(), 756872327473);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function arrangedProbability() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
arrangedProbability();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d21000cf542c50fee4
|
||||
title: 问题101:最佳多项式
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-101-optimum-polynomial
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -21,5 +22,21 @@ OP(1,n)= 11 1,1,1,1 ...... OP(2,n)= 7n-6 1,8,15,...... OP(3,n
|
||||
assert.strictEqual(euler101(), 37076114526);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler101() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler101();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d21000cf542c50fee5
|
||||
title: 问题102:三角形遏制
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-102-triangle-containment
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler102(), 228);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler102() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler102();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d61000cf542c50fee7
|
||||
title: 问题103:特殊子集和:最佳
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-103-special-subset-sums-optimum
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler103(), 20313839404245);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler103() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler103();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d51000cf542c50fee6
|
||||
title: 问题104:Pandigital Fibonacci结束
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-104-pandigital-fibonacci-ends
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ Fibonacci序列由递归关系定义:Fn = Fn-1 + Fn-2,其中F1 = 1且F2 = 1.
|
||||
assert.strictEqual(euler104(), 329468);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler104() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler104();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d61000cf542c50fee8
|
||||
title: 问题105:特殊子集总和:测试
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-105-special-subset-sums-testing
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler105(), 73702);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler105() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler105();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d71000cf542c50fee9
|
||||
title: 问题106:特殊子集和:元测试
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-106-special-subset-sums-meta-testing
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler106(), 21384);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler106() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler106();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d91000cf542c50feea
|
||||
title: 问题107:最小网络
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-107-minimal-network
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -21,5 +22,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler107(), 259679);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler107() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler107();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3d91000cf542c50feeb
|
||||
title: 问题108:丢番图互惠I
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-108-diophantine-reciprocals-i
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ diophantineOne `diophantineOne()`应返回180180。
|
||||
assert.strictEqual(diophantineOne(), 180180);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function diophantineOne() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
diophantineOne();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3db1000cf542c50feec
|
||||
title: 问题109:飞镖
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-109-darts
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -33,5 +34,21 @@ S1 S1 D2 S1 T1 D1 S1 S3 D1 D1 D1 D1 D1 S2 D1 S2 S2 D1
|
||||
assert.strictEqual(euler109(), 38182);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler109() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler109();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3781000cf542c50fe8a
|
||||
title: 问题11:网格中最大的产品
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-11-largest-product-in-a-grid
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -65,5 +66,126 @@ assert.strictEqual(largestGridProduct(grid), 70600674);
|
||||
assert.strictEqual(largestGridProduct(testGrid), 14169081);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largestGridProduct(arr) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
const grid = [
|
||||
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
||||
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
||||
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
||||
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
|
||||
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
|
||||
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
|
||||
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
|
||||
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
|
||||
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
|
||||
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
|
||||
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
|
||||
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
|
||||
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
|
||||
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
|
||||
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
|
||||
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
|
||||
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
|
||||
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
|
||||
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
||||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
|
||||
];
|
||||
|
||||
const testGrid = [
|
||||
[40, 17, 81, 18, 57],
|
||||
[74, 4, 36, 16, 29],
|
||||
[36, 42, 69, 73, 45],
|
||||
[51, 54, 69, 16, 92],
|
||||
[7, 97, 57, 32, 16]
|
||||
];
|
||||
|
||||
largestGridProduct(testGrid);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function largestGridProduct(arr) {
|
||||
let maxProduct = 0;
|
||||
let currProduct = 0;
|
||||
|
||||
function maxProductChecker(n) {
|
||||
if (n > maxProduct) {
|
||||
return maxProduct = n;
|
||||
}
|
||||
}
|
||||
|
||||
// loop rows
|
||||
for (let r = 0; r < arr.length; r++) {
|
||||
// loop columns
|
||||
for (let c = 0; c < arr[r].length; c++) {
|
||||
const limit = arr[r].length - 3;
|
||||
|
||||
// check horizontal
|
||||
if (c < limit) {
|
||||
currProduct = arr[r][c] * arr[r][c + 1] * arr[r][c + 2] * arr[r][c + 3];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
|
||||
// check vertical
|
||||
if (r < limit) {
|
||||
currProduct = arr[r][c] * arr[r + 1][c] * arr[r + 2][c] * arr[r + 3][c];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
|
||||
// check diagonal [\]
|
||||
if (c < limit && r < limit) {
|
||||
currProduct = arr[r][c] * arr[r + 1][c + 1] * arr[r + 2][c + 2] * arr[r + 3][c + 3];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
|
||||
// check diagonal [/]
|
||||
if (c > 3 && r < limit) {
|
||||
currProduct = arr[r][c] * arr[r + 1][c - 1] * arr[r + 2][c - 2] * arr[r + 3][c - 3];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxProduct;
|
||||
}
|
||||
|
||||
const grid = [ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
||||
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
||||
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
||||
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
|
||||
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
|
||||
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
|
||||
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
|
||||
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
|
||||
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
|
||||
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
|
||||
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
|
||||
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
|
||||
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
|
||||
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
|
||||
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
|
||||
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
|
||||
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
|
||||
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
|
||||
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
||||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
|
||||
];
|
||||
|
||||
const testGrid = [
|
||||
[40, 17, 81, 18, 57],
|
||||
[74, 4, 36, 16, 29],
|
||||
[36, 42, 69, 73, 45],
|
||||
[51, 54, 69, 16, 92],
|
||||
[7, 97, 57, 32, 16]
|
||||
];
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3db1000cf542c50feed
|
||||
title: 问题110:丢番图互惠II
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-110-diophantine-reciprocals-ii
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(diophantineTwo(), 9350130049860600);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function diophantineTwo() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
diophantineTwo();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3db1000cf542c50feee
|
||||
title: 问题111:运行的Primes
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-111-primes-with-runs
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -21,5 +22,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler111(), 612407567715);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler111() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler111();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3dd1000cf542c50feef
|
||||
title: 问题112:有弹性的数字
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-112-bouncy-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler112(), 1587000);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler112() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler112();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3dd1000cf542c50fef0
|
||||
title: 问题113:非弹性数字
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-113-non-bouncy-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler113(), 51161058134250);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler113() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler113();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e01000cf542c50fef2
|
||||
title: 问题114:计数块组合I
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-114-counting-block-combinations-i
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler114(), 16475640049);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler114() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler114();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3df1000cf542c50fef1
|
||||
title: 问题115:计数块组合II
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-115-counting-block-combinations-ii
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler115(), 168);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler115() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler115();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e01000cf542c50fef3
|
||||
title: 问题116:红色,绿色或蓝色瓷砖
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-116-red-green-or-blue-tiles
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -23,5 +24,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler116(), 20492570929);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler116() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler116();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e21000cf542c50fef4
|
||||
title: 问题117:红色,绿色和蓝色瓷砖
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-117-red-green-and-blue-tiles
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler117(), 100808458960497);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler117() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler117();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e21000cf542c50fef5
|
||||
title: 问题118:Pandigital prime set
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-118-pandigital-prime-sets
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler118(), 44680);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler118() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler118();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e41000cf542c50fef6
|
||||
title: 问题119:数字功率总和
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-119-digit-power-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler119(), 248155780267521);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler119() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler119();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3781000cf542c50fe8b
|
||||
title: 问题12:高度可分的三角数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-12-highly-divisible-triangular-number
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -61,5 +62,76 @@ divisibleTriangleNumber `divisibleTriangleNumber(500)`应该返回76576500。
|
||||
assert.strictEqual(divisibleTriangleNumber(500), 76576500);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function divisibleTriangleNumber(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
divisibleTriangleNumber(500);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function divisibleTriangleNumber(n) {
|
||||
if (n === 1) return 3;
|
||||
let counter = 1;
|
||||
let triangleNumber = counter++;
|
||||
|
||||
|
||||
while (noOfFactors(triangleNumber) < n) {
|
||||
triangleNumber += counter++;
|
||||
}
|
||||
return triangleNumber;
|
||||
}
|
||||
|
||||
function noOfFactors(num) {
|
||||
const primeFactors = getPrimeFactors(num);
|
||||
let prod = 1;
|
||||
for(let p in primeFactors) {
|
||||
prod *= (primeFactors[p] + 1)
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
|
||||
function getPrimeFactors(num) {
|
||||
let n = num;
|
||||
let primes = {};
|
||||
|
||||
let p = 2;
|
||||
let sqrt = Math.sqrt(num);
|
||||
|
||||
function checkAndUpdate(inc) {
|
||||
if (n % p === 0) {
|
||||
const curr = primes[p];
|
||||
if (curr) {
|
||||
primes[p]++
|
||||
} else {
|
||||
primes[p] = 1;
|
||||
}
|
||||
n /= p;
|
||||
} else {
|
||||
p += inc;
|
||||
}
|
||||
}
|
||||
|
||||
while(p === 2 && p <= n) {
|
||||
checkAndUpdate(1);
|
||||
}
|
||||
|
||||
while (p <= n && p <= sqrt) {
|
||||
checkAndUpdate(2);
|
||||
}
|
||||
if(Object.keys(primes).length === 0) {
|
||||
primes[num] = 1;
|
||||
} else if(n !== 1) {
|
||||
primes[n] = 1;
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e41000cf542c50fef7
|
||||
title: 问题120:方形剩余部分
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-120-square-remainders
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler120(), 333082500);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler120() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler120();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e51000cf542c50fef8
|
||||
title: 问题121:光盘游戏奖金
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-121-disc-game-prize-fund
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler121(), 2269);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler121() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler121();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e61000cf542c50fef9
|
||||
title: 问题122:有效取幂
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-122-efficient-exponentiation
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler122(), 1582);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler122() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler122();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e71000cf542c50fefa
|
||||
title: 问题123:素数正方形余数
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-123-prime-square-remainders
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler123(), 21035);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler123() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler123();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e81000cf542c50fefb
|
||||
title: 问题124:有序的激进分子
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-124-ordered-radicals
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -41,5 +42,21 @@ n rad(n)k 11
|
||||
assert.strictEqual(euler124(), 21417);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler124() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler124();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3e91000cf542c50fefc
|
||||
title: 问题125:回文总和
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-125-palindromic-sums
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler125(), 2906969179);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler125() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler125();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3ea1000cf542c50fefd
|
||||
title: 问题126:长方体层
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-126-cuboid-layers
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler126(), 18522);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler126() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler126();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3ec1000cf542c50fefe
|
||||
title: 问题127:abc-hits
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-127-abc-hits
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ n,rad(n)的基数是n的不同素因子的乘积。例如,504 = 23×32×
|
||||
assert.strictEqual(euler127(), 18407904);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler127() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler127();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3ec1000cf542c50feff
|
||||
title: 问题128:六边形瓷砖差异
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-128-hexagonal-tile-differences
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler128(), 14516824220);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler128() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler128();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3ef1000cf542c50ff01
|
||||
title: 问题129:重新划分可分性
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-129-repunit-divisibility
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler129(), 1000023);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler129() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler129();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f37a1000cf542c50fe8c
|
||||
title: 问题13:大笔金额
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-13-large-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -25,5 +26,154 @@ assert.strictEqual(largeSum(testNums), 8348422521);
|
||||
assert.strictEqual(largeSum(fiftyDigitNums), 5537376230);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
const fiftyDigitNums = [
|
||||
'37107287533902102798797998220837590246510135740250',
|
||||
'46376937677490009712648124896970078050417018260538',
|
||||
'74324986199524741059474233309513058123726617309629',
|
||||
'91942213363574161572522430563301811072406154908250',
|
||||
'23067588207539346171171980310421047513778063246676',
|
||||
'89261670696623633820136378418383684178734361726757',
|
||||
'28112879812849979408065481931592621691275889832738',
|
||||
'44274228917432520321923589422876796487670272189318',
|
||||
'47451445736001306439091167216856844588711603153276',
|
||||
'70386486105843025439939619828917593665686757934951',
|
||||
'62176457141856560629502157223196586755079324193331',
|
||||
'64906352462741904929101432445813822663347944758178',
|
||||
'92575867718337217661963751590579239728245598838407',
|
||||
'58203565325359399008402633568948830189458628227828',
|
||||
'80181199384826282014278194139940567587151170094390',
|
||||
'35398664372827112653829987240784473053190104293586',
|
||||
'86515506006295864861532075273371959191420517255829',
|
||||
'71693888707715466499115593487603532921714970056938',
|
||||
'54370070576826684624621495650076471787294438377604',
|
||||
'53282654108756828443191190634694037855217779295145',
|
||||
'36123272525000296071075082563815656710885258350721',
|
||||
'45876576172410976447339110607218265236877223636045',
|
||||
'17423706905851860660448207621209813287860733969412',
|
||||
'81142660418086830619328460811191061556940512689692',
|
||||
'51934325451728388641918047049293215058642563049483',
|
||||
'62467221648435076201727918039944693004732956340691',
|
||||
'15732444386908125794514089057706229429197107928209',
|
||||
'55037687525678773091862540744969844508330393682126',
|
||||
'18336384825330154686196124348767681297534375946515',
|
||||
'80386287592878490201521685554828717201219257766954',
|
||||
'78182833757993103614740356856449095527097864797581',
|
||||
'16726320100436897842553539920931837441497806860984',
|
||||
'48403098129077791799088218795327364475675590848030',
|
||||
'87086987551392711854517078544161852424320693150332',
|
||||
'59959406895756536782107074926966537676326235447210',
|
||||
'69793950679652694742597709739166693763042633987085',
|
||||
'41052684708299085211399427365734116182760315001271',
|
||||
'65378607361501080857009149939512557028198746004375',
|
||||
'35829035317434717326932123578154982629742552737307',
|
||||
'94953759765105305946966067683156574377167401875275',
|
||||
'88902802571733229619176668713819931811048770190271',
|
||||
'25267680276078003013678680992525463401061632866526',
|
||||
'36270218540497705585629946580636237993140746255962',
|
||||
'24074486908231174977792365466257246923322810917141',
|
||||
'91430288197103288597806669760892938638285025333403',
|
||||
'34413065578016127815921815005561868836468420090470',
|
||||
'23053081172816430487623791969842487255036638784583',
|
||||
'11487696932154902810424020138335124462181441773470',
|
||||
'63783299490636259666498587618221225225512486764533',
|
||||
'67720186971698544312419572409913959008952310058822',
|
||||
'95548255300263520781532296796249481641953868218774',
|
||||
'76085327132285723110424803456124867697064507995236',
|
||||
'37774242535411291684276865538926205024910326572967',
|
||||
'23701913275725675285653248258265463092207058596522',
|
||||
'29798860272258331913126375147341994889534765745501',
|
||||
'18495701454879288984856827726077713721403798879715',
|
||||
'38298203783031473527721580348144513491373226651381',
|
||||
'34829543829199918180278916522431027392251122869539',
|
||||
'40957953066405232632538044100059654939159879593635',
|
||||
'29746152185502371307642255121183693803580388584903',
|
||||
'41698116222072977186158236678424689157993532961922',
|
||||
'62467957194401269043877107275048102390895523597457',
|
||||
'23189706772547915061505504953922979530901129967519',
|
||||
'86188088225875314529584099251203829009407770775672',
|
||||
'11306739708304724483816533873502340845647058077308',
|
||||
'82959174767140363198008187129011875491310547126581',
|
||||
'97623331044818386269515456334926366572897563400500',
|
||||
'42846280183517070527831839425882145521227251250327',
|
||||
'55121603546981200581762165212827652751691296897789',
|
||||
'32238195734329339946437501907836945765883352399886',
|
||||
'75506164965184775180738168837861091527357929701337',
|
||||
'62177842752192623401942399639168044983993173312731',
|
||||
'32924185707147349566916674687634660915035914677504',
|
||||
'99518671430235219628894890102423325116913619626622',
|
||||
'73267460800591547471830798392868535206946944540724',
|
||||
'76841822524674417161514036427982273348055556214818',
|
||||
'97142617910342598647204516893989422179826088076852',
|
||||
'87783646182799346313767754307809363333018982642090',
|
||||
'10848802521674670883215120185883543223812876952786',
|
||||
'71329612474782464538636993009049310363619763878039',
|
||||
'62184073572399794223406235393808339651327408011116',
|
||||
'66627891981488087797941876876144230030984490851411',
|
||||
'60661826293682836764744779239180335110989069790714',
|
||||
'85786944089552990653640447425576083659976645795096',
|
||||
'66024396409905389607120198219976047599490197230297',
|
||||
'64913982680032973156037120041377903785566085089252',
|
||||
'16730939319872750275468906903707539413042652315011',
|
||||
'94809377245048795150954100921645863754710598436791',
|
||||
'78639167021187492431995700641917969777599028300699',
|
||||
'15368713711936614952811305876380278410754449733078',
|
||||
'40789923115535562561142322423255033685442488917353',
|
||||
'44889911501440648020369068063960672322193204149535',
|
||||
'41503128880339536053299340368006977710650566631954',
|
||||
'81234880673210146739058568557934581403627822703280',
|
||||
'82616570773948327592232845941706525094512325230608',
|
||||
'22918802058777319719839450180888072429661980811197',
|
||||
'77158542502016545090413245809786882778948721859617',
|
||||
'72107838435069186155435662884062257473692284509516',
|
||||
'20849603980134001723930671666823555245252804609722',
|
||||
'53503534226472524250874054075591789781264330331690'
|
||||
];
|
||||
|
||||
const testNums = [
|
||||
'37107287533902102798797998220837590246510135740250',
|
||||
'46376937677490009712648124896970078050417018260538'
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largeSum(arr) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const testNums = [
|
||||
'37107287533902102798797998220837590246510135740250',
|
||||
'46376937677490009712648124896970078050417018260538'
|
||||
];
|
||||
|
||||
largeSum(testNums);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function largeSum(arr) {
|
||||
|
||||
let sum = 0;
|
||||
|
||||
arr.forEach(function(num) {
|
||||
sum += parseInt(num, 10);
|
||||
});
|
||||
|
||||
sum = sum.toString(10);
|
||||
|
||||
sum = sum.substr(0, 1) + sum.substr(2);
|
||||
|
||||
let firstTen = sum.slice(0, 10);
|
||||
return parseInt(firstTen, 10);
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3ee1000cf542c50ff00
|
||||
title: 问题130:具有主要repunit属性的复合材料
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-130-composites-with-prime-repunit-property
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler130(), 149253);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler130() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler130();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3ef1000cf542c50ff02
|
||||
title: 问题131:Prime立方体伙伴关系
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-131-prime-cube-partnership
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler131(), 173);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler131() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler131();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f11000cf542c50ff03
|
||||
title: 问题132:大的重新安置因素
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-132-large-repunit-factors
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler132(), 843296);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler132() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler132();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f21000cf542c50ff04
|
||||
title: 问题133:重新计算非因素
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-133-repunit-nonfactors
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler133(), 453647705);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler133() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler133();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f21000cf542c50ff05
|
||||
title: 问题134:素对对连接
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-134-prime-pair-connection
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler134(), 18613426663617120);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler134() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler134();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f31000cf542c50ff06
|
||||
title: 问题135:同样的差异
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-135-same-differences
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler135(), 4989);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler135() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler135();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f51000cf542c50ff07
|
||||
title: 问题136:单身人士差异
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-136-singleton-difference
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -17,5 +18,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler136(), 2544559);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler136() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler136();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f51000cf542c50ff08
|
||||
title: 问题137:斐波那契金块
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-137-fibonacci-golden-nuggets
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -25,5 +26,21 @@ xAF(x)√2-111/ 22(√13-2)/ 33(√89-5)/ 84(√34-3)/ 55
|
||||
assert.strictEqual(euler137(), 1120149658760);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler137() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler137();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f61000cf542c50ff09
|
||||
title: 问题138:特殊的等腰三角形
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-138-special-isosceles-triangles
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler138(), 1118049290473932);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler138() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler138();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 5900f3f71000cf542c50ff0a
|
||||
title: 问题139:毕达哥拉斯瓷砖
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: problem-139-pythagorean-tiles
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -19,5 +20,21 @@ videoUrl: ''
|
||||
assert.strictEqual(euler139(), 10057761);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function euler139() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
euler139();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user