Skip to content

Commit

Permalink
Fomrat the JS and TS codes with prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Apr 17, 2023
1 parent 9a98ff8 commit c4ea4e3
Show file tree
Hide file tree
Showing 68 changed files with 634 additions and 510 deletions.
6 changes: 6 additions & 0 deletions codes/javascript/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": true
}
14 changes: 7 additions & 7 deletions codes/javascript/chapter_array_and_linkedlist/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ function find(nums, target) {
/* Driver Codes*/
/* 初始化数组 */
const arr = new Array(5).fill(0);
console.log("数组 arr =", arr);
console.log('数组 arr =', arr);
let nums = [1, 3, 2, 5, 4];
console.log("数组 nums =", nums);
console.log('数组 nums =', nums);

/* 随机访问 */
let random_num = randomAccess(nums);
console.log("在 nums 中获取随机元素", random_num);
console.log('在 nums 中获取随机元素', random_num);

/* 长度扩展 */
nums = extend(nums, 3);
console.log("将数组长度扩展至 8 ,得到 nums =", nums);
console.log('将数组长度扩展至 8 ,得到 nums =', nums);

/* 插入元素 */
insert(nums, 6, 3);
console.log("在索引 3 处插入数字 6 ,得到 nums =", nums);
console.log('在索引 3 处插入数字 6 ,得到 nums =', nums);

/* 删除元素 */
remove(nums, 2);
console.log("删除索引 2 处的元素,得到 nums =", nums);
console.log('删除索引 2 处的元素,得到 nums =', nums);

/* 遍历数组 */
traverse(nums);

/* 查找元素 */
let index = find(nums, 3);
console.log("在 nums 中查找元素 3 ,得到索引 =", index);
console.log('在 nums 中查找元素 3 ,得到索引 =', index);
17 changes: 8 additions & 9 deletions codes/javascript/chapter_array_and_linkedlist/linked_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Author: IsChristina ([email protected]), Justin ([email protected])
*/

const { printLinkedList } = require("../modules/PrintUtil");
const { ListNode } = require("../modules/ListNode");
const { printLinkedList } = require('../modules/PrintUtil');
const { ListNode } = require('../modules/ListNode');

/* 在链表的节点 n0 之后插入节点 P */
function insert(n0, P) {
Expand All @@ -16,8 +16,7 @@ function insert(n0, P) {

/* 删除链表的节点 n0 之后的首个节点 */
function remove(n0) {
if (!n0.next)
return;
if (!n0.next) return;
// n0 -> P -> n1
const P = n0.next;
const n1 = P.next;
Expand Down Expand Up @@ -61,23 +60,23 @@ n0.next = n1;
n1.next = n2;
n2.next = n3;
n3.next = n4;
console.log("初始化的链表为");
console.log('初始化的链表为');
printLinkedList(n0);

/* 插入节点 */
insert(n0, new ListNode(0));
console.log("插入节点后的链表为");
console.log('插入节点后的链表为');
printLinkedList(n0);

/* 删除节点 */
remove(n0);
console.log("删除节点后的链表为");
console.log('删除节点后的链表为');
printLinkedList(n0);

/* 访问节点 */
const node = access(n0, 3);
console.log("链表中索引 3 处的节点的值 = " + node.val);
console.log('链表中索引 3 处的节点的值 = ' + node.val);

/* 查找节点 */
const index = find(n0, 2);
console.log("链表中值为 2 的节点的索引 = " + index);
console.log('链表中值为 2 的节点的索引 = ' + index);
12 changes: 4 additions & 8 deletions codes/javascript/chapter_array_and_linkedlist/my_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ class MyList {
/* 访问元素 */
get(index) {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
if (index < 0 || index >= this.#size) throw new Error('索引越界');
return this.#nums[index];
}

/* 更新元素 */
set(index, num) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
if (index < 0 || index >= this.#size) throw new Error('索引越界');
this.#nums[index] = num;
}

Expand All @@ -54,8 +52,7 @@ class MyList {

/* 中间插入元素 */
insert(index, num) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
if (index < 0 || index >= this.#size) throw new Error('索引越界');
// 元素数量超出容量时,触发扩容机制
if (this.#size === this.#capacity) {
this.extendCapacity();
Expand All @@ -71,8 +68,7 @@ class MyList {

/* 删除元素 */
remove(index) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
if (index < 0 || index >= this.#size) throw new Error('索引越界');
let num = this.#nums[index];
// 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this.#size - 1; j++) {
Expand Down
35 changes: 21 additions & 14 deletions codes/javascript/chapter_binary_search/binary_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
/* 二分查找(双闭区间) */
function binarySearch(nums, target) {
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
let i = 0, j = nums.length - 1;
let i = 0,
j = nums.length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j) {
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
// 计算中点索引 m ,使用 parseInt() 向下取整
const m = parseInt((i + j) / 2);
if (nums[m] < target)
// 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
else if (nums[m] > target)
// 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else
return m; // 找到目标元素,返回其索引
else return m; // 找到目标元素,返回其索引
}
// 未找到目标元素,返回 -1
return -1;
Expand All @@ -25,16 +28,20 @@ function binarySearch(nums, target) {
/* 二分查找(左闭右开) */
function binarySearch1(nums, target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
let i = 0, j = nums.length;
let i = 0,
j = nums.length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
// 计算中点索引 m ,使用 parseInt() 向下取整
const m = parseInt((i + j) / 2);
if (nums[m] < target)
// 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
else if (nums[m] > target)
// 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
return m;
// 找到目标元素,返回其索引
else return m;
}
// 未找到目标元素,返回 -1
return -1;
Expand All @@ -46,8 +53,8 @@ const nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92];

/* 二分查找(双闭区间) */
let index = binarySearch(nums, target);
console.log("目标元素 6 的索引 = " + index);
console.log('目标元素 6 的索引 = ' + index);

/* 二分查找(左闭右开) */
index = binarySearch1(nums, target);
console.log("目标元素 6 的索引 = " + index);
console.log('目标元素 6 的索引 = ' + index);
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function linearRecur(n) {
/* 平方阶 */
function quadratic(n) {
// 矩阵占用 O(n^2) 空间
const numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));
const numMatrix = Array(n)
.fill(null)
.map(() => Array(n).fill(null));
// 二维列表占用 O(n^2) 空间
const numList = [];
for (let i = 0; i < n; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,35 @@ function factorialRecur(n) {
/* Driver Code */
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
const n = 8;
console.log("输入数据大小 n = " + n);
console.log('输入数据大小 n = ' + n);

let count = constant(n);
console.log("常数阶的计算操作数量 = " + count);
console.log('常数阶的计算操作数量 = ' + count);

count = linear(n);
console.log("线性阶的计算操作数量 = " + count);
console.log('线性阶的计算操作数量 = ' + count);
count = arrayTraversal(new Array(n));
console.log("线性阶(遍历数组)的计算操作数量 = " + count);
console.log('线性阶(遍历数组)的计算操作数量 = ' + count);

count = quadratic(n);
console.log("平方阶的计算操作数量 = " + count);
console.log('平方阶的计算操作数量 = ' + count);
let nums = new Array(n);
for (let i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
console.log("平方阶(冒泡排序)的计算操作数量 = " + count);
console.log('平方阶(冒泡排序)的计算操作数量 = ' + count);

count = exponential(n);
console.log("指数阶(循环实现)的计算操作数量 = " + count);
console.log('指数阶(循环实现)的计算操作数量 = ' + count);
count = expRecur(n);
console.log("指数阶(递归实现)的计算操作数量 = " + count);
console.log('指数阶(递归实现)的计算操作数量 = ' + count);

count = logarithmic(n);
console.log("对数阶(循环实现)的计算操作数量 = " + count);
console.log('对数阶(循环实现)的计算操作数量 = ' + count);
count = logRecur(n);
console.log("对数阶(递归实现)的计算操作数量 = " + count);
console.log('对数阶(递归实现)的计算操作数量 = ' + count);

count = linearLogRecur(n);
console.log("线性对数阶(递归实现)的计算操作数量 = " + count);
console.log('线性对数阶(递归实现)的计算操作数量 = ' + count);

count = factorialRecur(n);
console.log("阶乘阶(递归实现)的计算操作数量 = " + count);
console.log('阶乘阶(递归实现)的计算操作数量 = ' + count);
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ for (let i = 0; i < 10; i++) {
const n = 100;
const nums = randomNumbers(n);
const index = findOne(nums);
console.log(
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
);
console.log("数字 1 的索引为 " + index);
console.log('\n数组 [ 1, 2, ..., n ] 被打乱后 = [' + nums.join(', ') + ']');
console.log('数字 1 的索引为 ' + index);
}
46 changes: 30 additions & 16 deletions codes/javascript/chapter_graph/graph_adjacency_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: Justin ([email protected])
*/

const { Vertex } = require('../modules/Vertex')
const { Vertex } = require('../modules/Vertex');

/* 基于邻接表实现的无向图类 */
class GraphAdjList {
Expand All @@ -29,8 +29,12 @@ class GraphAdjList {

/* 添加边 */
addEdge(vet1, vet2) {
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
throw new Error("Illegal Argument Exception");
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
) {
throw new Error('Illegal Argument Exception');
}
// 添加边 vet1 - vet2
this.adjList.get(vet1).push(vet2);
Expand All @@ -39,8 +43,12 @@ class GraphAdjList {

/* 删除边 */
removeEdge(vet1, vet2) {
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
throw new Error("Illegal Argument Exception");
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2
) {
throw new Error('Illegal Argument Exception');
}
// 删除边 vet1 - vet2
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);
Expand All @@ -57,7 +65,7 @@ class GraphAdjList {
/* 删除顶点 */
removeVertex(vet) {
if (!this.adjList.has(vet)) {
throw new Error("Illegal Argument Exception");
throw new Error('Illegal Argument Exception');
}
// 在邻接表中删除顶点 vet 对应的链表
this.adjList.delete(vet);
Expand All @@ -72,13 +80,13 @@ class GraphAdjList {

/* 打印邻接表 */
print() {
console.log("邻接表 =");
console.log('邻接表 =');
for (const [key, value] of this.adjList) {
const tmp = [];
for (const vertex of value) {
tmp.push(vertex.val);
}
console.log(key.val + ": " + tmp.join());
console.log(key.val + ': ' + tmp.join());
}
}
}
Expand All @@ -91,37 +99,43 @@ if (require.main === module) {
v2 = new Vertex(2),
v3 = new Vertex(5),
v4 = new Vertex(4);
const edges = [[v0, v1], [v1, v2], [v2, v3], [v0, v3], [v2, v4], [v3, v4]];
const edges = [
[v0, v1],
[v1, v2],
[v2, v3],
[v0, v3],
[v2, v4],
[v3, v4],
];
const graph = new GraphAdjList(edges);
console.log("\n初始化后,图为");
console.log('\n初始化后,图为');
graph.print();

/* 添加边 */
// 顶点 1, 2 即 v0, v2
graph.addEdge(v0, v2);
console.log("\n添加边 1-2 后,图为");
console.log('\n添加边 1-2 后,图为');
graph.print();

/* 删除边 */
// 顶点 1, 3 即 v0, v1
graph.removeEdge(v0, v1);
console.log("\n删除边 1-3 后,图为");
console.log('\n删除边 1-3 后,图为');
graph.print();

/* 添加顶点 */
const v5 = new Vertex(6);
graph.addVertex(v5);
console.log("\n添加顶点 6 后,图为");
console.log('\n添加顶点 6 后,图为');
graph.print();

/* 删除顶点 */
// 顶点 3 即 v1
graph.removeVertex(v1);
console.log("\n删除顶点 3 后,图为");
console.log('\n删除顶点 3 后,图为');
graph.print();

}

module.exports = {
GraphAdjList
GraphAdjList,
};
Loading

0 comments on commit c4ea4e3

Please sign in to comment.