mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-29 13:13:54 +08:00
Categorize scripts
This commit is contained in:
26
utilities/javascript/binarySearch.js
Normal file
26
utilities/javascript/binarySearch.js
Normal file
@ -0,0 +1,26 @@
|
||||
function binarySearch(arr, target) {
|
||||
let left = 0;
|
||||
let right = arr.length - 1;
|
||||
while (left < right) {
|
||||
let mid = left + Math.floor((right - left) / 2);
|
||||
if (arr[mid] === target) {
|
||||
return mid;
|
||||
}
|
||||
if (arr[mid] < target) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
console.log(binarySearch([1, 2, 3, 10], 1) === 0)
|
||||
console.log(binarySearch([1, 2, 3, 10], 2) === 1)
|
||||
console.log(binarySearch([1, 2, 3, 10], 3) === 2)
|
||||
console.log(binarySearch([1, 2, 3, 10], 10) === 3)
|
||||
console.log(binarySearch([1, 2, 3, 10], 9) === 3)
|
||||
console.log(binarySearch([1, 2, 3, 10], 4) === 3)
|
||||
console.log(binarySearch([1, 2, 3, 10], 0) === 0)
|
||||
console.log(binarySearch([1, 2, 3, 10], 11) === 3)
|
||||
console.log(binarySearch([5, 7, 8, 10], 3) === 0)
|
35
utilities/javascript/graphTopoSort.js
Normal file
35
utilities/javascript/graphTopoSort.js
Normal file
@ -0,0 +1,35 @@
|
||||
function graphTopoSort(numberNodes, edges) {
|
||||
const nodes = new Map();
|
||||
const order = [];
|
||||
const queue = [];
|
||||
for (let i = 0; i < numberNodes; i++) {
|
||||
nodes.set(i, { in: 0, out: new Set() });
|
||||
}
|
||||
|
||||
edges.forEach(edge => {
|
||||
const [node_id, pre_id] = edge;
|
||||
nodes.get(node_id).in += 1;
|
||||
nodes.get(pre_id).out.add(node_id);
|
||||
});
|
||||
|
||||
for (let [node_id, value] of nodes.entries()) {
|
||||
if (value.in === 0) {
|
||||
queue.push(node_id);
|
||||
}
|
||||
}
|
||||
|
||||
while (queue.length) {
|
||||
const node_id = queue.shift();
|
||||
for (let outgoing_id of nodes.get(node_id).out) {
|
||||
nodes.get(outgoing_id).in -= 1;
|
||||
if (nodes.get(outgoing_id).in === 0) {
|
||||
queue.push(outgoing_id);
|
||||
}
|
||||
}
|
||||
order.push(node_id);
|
||||
}
|
||||
|
||||
return order.length == numberNodes ? order : [];
|
||||
}
|
||||
|
||||
console.log(graphTopoSort(3, [[0, 1], [0, 2]]))
|
19
utilities/javascript/isSubsequence.js
Normal file
19
utilities/javascript/isSubsequence.js
Normal file
@ -0,0 +1,19 @@
|
||||
function isSubsequence(s, t) {
|
||||
if (s.length > t.length) {
|
||||
return false;
|
||||
}
|
||||
let matchedLength = 0;
|
||||
for (let i = 0; i < t.length; i++) {
|
||||
if (matchedLength < s.length && s[matchedLength] === t[i]) {
|
||||
matchedLength += 1;
|
||||
}
|
||||
}
|
||||
return matchedLength === s.length;
|
||||
}
|
||||
|
||||
console.log(isSubsequence('abc', 'abcde') === true);
|
||||
console.log(isSubsequence('abd', 'abcde') === true);
|
||||
console.log(isSubsequence('abf', 'abcde') === false);
|
||||
console.log(isSubsequence('abef', 'abcde') === false);
|
||||
console.log(isSubsequence('abcdef', 'abcde') === false);
|
||||
console.log(isSubsequence('a', 'abcde') === true);
|
11
utilities/javascript/treeEqual.js
Normal file
11
utilities/javascript/treeEqual.js
Normal file
@ -0,0 +1,11 @@
|
||||
function treeEqual(node1, node2) {
|
||||
if (!node1 && !node2) {
|
||||
return true;
|
||||
}
|
||||
if (!node1 || !node2) {
|
||||
return false;
|
||||
}
|
||||
return node1.val == node2.val &&
|
||||
treeEqual(node1.left, node2.left) &&
|
||||
treeEqual(node1.right, node2.right);
|
||||
}
|
10
utilities/javascript/treeMirror.js
Normal file
10
utilities/javascript/treeMirror.js
Normal file
@ -0,0 +1,10 @@
|
||||
function treeMirror(node) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
let temp = node.left;
|
||||
node.left = node.right;
|
||||
node.right = temp;
|
||||
treeMirror(node.left);
|
||||
treeMirror(node.right);
|
||||
}
|
Reference in New Issue
Block a user