Add a ton of JS algo snippets (#56)
parent
d8c3f984e4
commit
d9b9d4b5ca
@ -0,0 +1,15 @@
|
||||
// Does not handle negative binary numbers.
|
||||
function binToInt(binary) {
|
||||
let res = 0;
|
||||
for (let i = 0; i < binary.length; i++) {
|
||||
res = res * 2 + (+binary[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
console.log(binToInt('0') === parseInt('0', 2) && parseInt('0', 2) === 0);
|
||||
console.log(binToInt('1') === parseInt('1', 2) && parseInt('1', 2) === 1);
|
||||
console.log(binToInt('10') === parseInt('10', 2) && parseInt('10', 2) === 2);
|
||||
console.log(binToInt('11') === parseInt('11', 2) && parseInt('11', 2) === 3);
|
||||
console.log(binToInt('101') === parseInt('101', 2) && parseInt('101', 2) === 5);
|
||||
console.log(binToInt('1100011') === parseInt('1100011', 2) && parseInt('1100011', 2) === 99);
|
@ -0,0 +1,19 @@
|
||||
// Does not handle negative numbers.
|
||||
function intToBin(number) {
|
||||
if (number === 0) {
|
||||
return '0';
|
||||
}
|
||||
let res = '';
|
||||
while (number > 0) {
|
||||
res = String(number % 2) + res;
|
||||
number = parseInt(number / 2, 10);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
console.log(intToBin(0) === 0..toString(2) && 0..toString(2) === '0');
|
||||
console.log(intToBin(1) === 1..toString(2) && 1..toString(2) === '1');
|
||||
console.log(intToBin(2) === 2..toString(2) && 2..toString(2) === '10');
|
||||
console.log(intToBin(3) === 3..toString(2) && 3..toString(2) === '11');
|
||||
console.log(intToBin(5) === 5..toString(2) && 5..toString(2) === '101');
|
||||
console.log(intToBin(99) === 99..toString(2) && 99..toString(2) === '1100011');
|
@ -0,0 +1,17 @@
|
||||
// Interval: [start, end].
|
||||
function intervalsIntersect(a, b) {
|
||||
return a[0] < b[1] && b[0] < a[1];
|
||||
}
|
||||
|
||||
console.log(intervalsIntersect([1, 2], [3, 4]) === false);
|
||||
console.log(intervalsIntersect([1, 2], [2, 4]) === false);
|
||||
console.log(intervalsIntersect([1, 2], [1, 4]) === true);
|
||||
console.log(intervalsIntersect([1, 2], [0, 4]) === true);
|
||||
console.log(intervalsIntersect([1, 2], [0, 2]) === true);
|
||||
console.log(intervalsIntersect([1, 2], [0, 1.5]) === true);
|
||||
console.log(intervalsIntersect([3, 4], [1, 2]) === false);
|
||||
console.log(intervalsIntersect([2, 4], [1, 2]) === false);
|
||||
console.log(intervalsIntersect([1, 4], [1, 2]) === true);
|
||||
console.log(intervalsIntersect([0, 4], [1, 2]) === true);
|
||||
console.log(intervalsIntersect([0, 2], [1, 2]) === true);
|
||||
console.log(intervalsIntersect([0, 1.5], [1, 2]) === true);
|
@ -0,0 +1,16 @@
|
||||
// Interval: [start, end].
|
||||
// Merges two overlapping intervals into one.
|
||||
function intervalsMerge(a, b) {
|
||||
return [Math.min(a[0], b[0]), Math.max(a[1], b[1])];
|
||||
}
|
||||
|
||||
const deepEqual = require('./deepEqual');
|
||||
|
||||
console.log(deepEqual(intervalsMerge([1, 2], [1, 4]), [1, 4]));
|
||||
console.log(deepEqual(intervalsMerge([1, 2], [0, 4]), [0, 4]));
|
||||
console.log(deepEqual(intervalsMerge([1, 2], [0, 2]), [0, 2]));
|
||||
console.log(deepEqual(intervalsMerge([1, 2], [0, 1.5]), [0, 2]));
|
||||
console.log(deepEqual(intervalsMerge([1, 4], [1, 2]), [1, 4]));
|
||||
console.log(deepEqual(intervalsMerge([0, 4], [1, 2]), [0, 4]));
|
||||
console.log(deepEqual(intervalsMerge([0, 2], [1, 2]), [0, 2]));
|
||||
console.log(deepEqual(intervalsMerge([0, 1.5], [1, 2]), [0, 2]));
|
@ -0,0 +1,18 @@
|
||||
function matrixClone(matrix, defaultValue) {
|
||||
return matrix.map(row => {
|
||||
return defaultValue === undefined ? row.slice(0) : Array(row.length).fill(defaultValue);
|
||||
});
|
||||
}
|
||||
|
||||
const deepEqual = require('./deepEqual');
|
||||
|
||||
// Test clone.
|
||||
const a = [[1, 2], [1, 4]];
|
||||
console.log(deepEqual(matrixClone(a), [[1, 2], [1, 4]]));
|
||||
a[0][0] = 4;
|
||||
console.log(deepEqual(matrixClone(a), [[1, 2], [1, 4]]) === false);
|
||||
console.log(deepEqual(matrixClone([[1]]), [[1]]));
|
||||
|
||||
// Test clone with default value.
|
||||
console.log(deepEqual(matrixClone([[1, 2], [1, 4]], 1), [[1, 1], [1, 1]]));
|
||||
console.log(deepEqual(matrixClone([[1, 2], [1, 4]], null), [[null, null], [null, null]]));
|
@ -0,0 +1,11 @@
|
||||
function matrixTranspose(matrix) {
|
||||
return matrix[0].map((col, i) => matrix.map(row => row[i]));
|
||||
}
|
||||
|
||||
const deepEqual = require('./deepEqual');
|
||||
|
||||
console.log(deepEqual(matrixTranspose([[1]]), [[1]]));
|
||||
console.log(deepEqual(matrixTranspose([[1, 2]]), [[1], [2]]));
|
||||
console.log(deepEqual(matrixTranspose([[1, 2], [1, 4]]), [[1, 1], [2, 4]]));
|
||||
console.log(deepEqual(matrixTranspose([[1, 2, 3], [4, 5, 6]]), [[1, 4], [2, 5], [3, 6]]));
|
||||
|
@ -0,0 +1,28 @@
|
||||
function traverse(matrix) {
|
||||
const DIRECTIONS = [[0, 1], [0, -1], [1, 0], [-1, 0]];
|
||||
const rows = matrix.length, cols = matrix[0].length;
|
||||
const visited = matrix.map(row => Array(row.length).fill(false));
|
||||
function dfs(i, j) {
|
||||
if (visited[i][j]) {
|
||||
return;
|
||||
}
|
||||
visited[i][j] = true;
|
||||
DIRECTIONS.forEach(dir => {
|
||||
const row = i + dir[0], col = j + dir[1];
|
||||
// Boundary check.
|
||||
if (row < 0 || row >= rows || col < 0 || col >= cols) {
|
||||
return;
|
||||
}
|
||||
// Valid neighbor check.
|
||||
if (matrix[row][col] !== 1) {
|
||||
return;
|
||||
}
|
||||
dfs(row, col);
|
||||
});
|
||||
}
|
||||
for (let i = 0; i < rows; i++) {
|
||||
for (let j = 0; j < cols; j++) {
|
||||
dfs(i, j);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue