Update TypeScript codes.

This commit is contained in:
Yudong Jin 2023-02-02 03:08:43 +08:00
parent 7d14c9440e
commit 29dbe8cd82
21 changed files with 94 additions and 62 deletions

4
codes/typescript/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules
out
package.json
package-lock.json

View File

@ -4,7 +4,7 @@
* Author: Justin (xiefahit@gmail.com)
*/
import ListNode from '../module/ListNode';
import { ListNode } from '../module/ListNode';
import { printLinkedList } from '../module/PrintUtil';
/* 在链表的结点 n0 之后插入结点 P */

View File

@ -41,4 +41,6 @@ console.log("方法一 res = ", res);
// 方法二
res = twoSumHashTable(nums, target);
console.log("方法二 res = ", res);
console.log("方法二 res = ", res);
export {};

View File

@ -153,3 +153,5 @@ console.log("线性对数阶(递归实现)的计算操作数量 = " + count)
count = factorialRecur(n);
console.log("阶乘阶(递归实现)的计算操作数量 = " + count);
export {};

View File

@ -32,14 +32,14 @@ function findOne(nums: number[]): number {
}
/* Driver Code */
function main(): void {
for (let i = 0; i < 10; i++) {
let n = 100;
let nums = randomNumbers(n);
let index = findOne(nums);
console.log(
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
);
console.log("数字 1 的索引为 " + index);
}
for (let i = 0; i < 10; i++) {
let n = 100;
let nums = randomNumbers(n);
let index = findOne(nums);
console.log(
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
);
console.log("数字 1 的索引为 " + index);
}
export {};

View File

@ -42,3 +42,5 @@ console.info('\n单独遍历值 Value');
for (const v of map.values()) {
console.info(v);
}
export {};

View File

@ -52,3 +52,5 @@ console.info('目标元素 6 的索引 = %d', index);
/* 二分查找(左闭右开) */
index = binarySearch1(nums, target);
console.info('目标元素 6 的索引 = %d', index);
export {};

View File

@ -5,7 +5,7 @@
*/
import { printLinkedList } from "../module/PrintUtil";
import ListNode from "../module/ListNode";
import { ListNode, arrToLinkedList } from "../module/ListNode";
/* 哈希查找(数组) */
@ -22,30 +22,29 @@ function hashingSearch1(map: Map<number, ListNode>, target: number): ListNode |
return map.has(target) ? map.get(target) as ListNode : null;
}
function main() {
const target = 3;
/* 哈希查找(数组) */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// 初始化哈希表
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: 元素value: 索引
}
const index = hashingSearch(map, target);
console.log("目标元素 3 的索引 = " + index);
/* Driver Code */
const target = 3;
/* 哈希查找(链表) */
let head = new ListNode().arrToLinkedList(nums)
// 初始化哈希表
const map1 = new Map();
while (head != null) {
map1.set(head.val, head); // key: 结点值value: 结点
head = head.next;
}
const node = hashingSearch1(map1, target);
console.log("目标结点值 3 的对应结点对象为");
printLinkedList(node);
/* 哈希查找(数组) */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// 初始化哈希表
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: 元素value: 索引
}
const index = hashingSearch(map, target);
console.log("目标元素 3 的索引 = " + index);
main();
/* 哈希查找(链表) */
let head = arrToLinkedList(nums)
// 初始化哈希表
const map1 = new Map();
while (head != null) {
map1.set(head.val, head); // key: 结点值value: 结点
head = head.next;
}
const node = hashingSearch1(map1, target);
console.log("目标结点值 3 的对应结点对象为", node);
export {};

View File

@ -4,7 +4,7 @@
* Author: Daniel (better.sunjian@gmail.com)
*/
import ListNode from '../module/ListNode.ts';
import { ListNode, arrToLinkedList } from '../module/ListNode';
/* 线性查找(数组)*/
function linearSearchArray(nums: number[], target: number): number {
@ -42,6 +42,8 @@ const index = linearSearchArray(nums, target);
console.log('目标元素 3 的索引 =', index);
/* 在链表中执行线性查找 */
const head = ListNode.arrToLinkedList(nums);
const head = arrToLinkedList(nums);
const node = linearSearchLinkedList(head, target);
console.log('目标结点值 3 的对应结点对象为', node);
export {};

View File

@ -102,7 +102,6 @@ for (let i = 0; i < 10; i++) {
queue.push(i);
queue.poll();
console.log("第 " + i + " 轮入队 + 出队后 queue =", queue.toArray());
console.log(queue.toArray());
}
export { };
export {};

View File

@ -79,4 +79,4 @@ console.log("栈的长度 size = " + size);
const empty = stack.empty();
console.log("栈是否为空 = " + empty);
export { };
export {};

View File

@ -4,7 +4,7 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
import ListNode from "../module/ListNode"
import { ListNode } from "../module/ListNode"
/* 基于链表实现的队列 */
class LinkedListQueue {
@ -100,3 +100,5 @@ console.log("队列长度 size = " + size);
/* 判断队列是否为空 */
const isEmpty = queue.isEmpty();
console.log("队列是否为空 = " + isEmpty);
export {};

View File

@ -4,7 +4,7 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
import ListNode from "../module/ListNode"
import { ListNode } from "../module/ListNode"
/* 基于链表实现的栈 */
class LinkedListStack {
@ -89,3 +89,5 @@ console.log("栈的长度 size = " + size);
/* 判断是否为空 */
const isEmpty = stack.isEmpty();
console.log("栈是否为空 = " + isEmpty);
export {};

View File

@ -28,4 +28,4 @@ const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;
export { };
export {};

View File

@ -27,4 +27,4 @@ const size = stack.length;
/* 判断是否为空 */
const is_empty = stack.length === 0;
export { };
export {};

View File

@ -33,3 +33,5 @@ printTree(n1);
n1.left = n2;
console.log('\n删除结点 P 后\n');
printTree(n1);
export {};

View File

@ -37,3 +37,5 @@ printTree(root);
/* 层序遍历 */
const list = hierOrder(root);
console.log('\n层序遍历的结点打印序列 = ' + list);
export {};

View File

@ -65,3 +65,5 @@ console.log('\n中序遍历的结点打印序列 = ' + list);
list.length = 0;
postOrder(root);
console.log('\n后序遍历的结点打印序列 = ' + list);
export {};

View File

@ -7,26 +7,28 @@
/**
* Definition for a singly-linked list node
*/
export default class ListNode {
class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
/**
* Generate a linked list with an array
* @param arr
* @return
*/
arrToLinkedList(arr: number[]): ListNode | null {
const dum: ListNode = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
}
/**
* Generate a linked list with an array
* @param arr
* @return
*/
function arrToLinkedList(arr: number[]): ListNode | null {
const dum: ListNode = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
export { ListNode, arrToLinkedList };

View File

@ -4,7 +4,7 @@
* Author: Justin (xiefahit@gmail.com)
*/
import ListNode from './ListNode';
import { ListNode } from './ListNode';
import { TreeNode } from './TreeNode';
/**

View File

@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "out",
"sourceMap": true
}
}