diff --git a/codes/typescript/.gitignore b/codes/typescript/.gitignore new file mode 100644 index 00000000..20170699 --- /dev/null +++ b/codes/typescript/.gitignore @@ -0,0 +1,4 @@ +node_modules +out +package.json +package-lock.json diff --git a/codes/typescript/chapter_array_and_linkedlist/linked_list.ts b/codes/typescript/chapter_array_and_linkedlist/linked_list.ts index b5eb988a..a5c49e8e 100644 --- a/codes/typescript/chapter_array_and_linkedlist/linked_list.ts +++ b/codes/typescript/chapter_array_and_linkedlist/linked_list.ts @@ -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 */ diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index 47c52d80..1759c089 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -41,4 +41,6 @@ console.log("方法一 res = ", res); // 方法二 res = twoSumHashTable(nums, target); -console.log("方法二 res = ", res); \ No newline at end of file +console.log("方法二 res = ", res); + +export {}; diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index e5dde67c..06beeede 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -153,3 +153,5 @@ console.log("线性对数阶(递归实现)的计算操作数量 = " + count) count = factorialRecur(n); console.log("阶乘阶(递归实现)的计算操作数量 = " + count); + +export {}; diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts index a7318634..fecf7c6f 100644 --- a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -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 {}; diff --git a/codes/typescript/chapter_hashing/hash_map.ts b/codes/typescript/chapter_hashing/hash_map.ts index 8d73da0c..0dea03d8 100644 --- a/codes/typescript/chapter_hashing/hash_map.ts +++ b/codes/typescript/chapter_hashing/hash_map.ts @@ -42,3 +42,5 @@ console.info('\n单独遍历值 Value'); for (const v of map.values()) { console.info(v); } + +export {}; diff --git a/codes/typescript/chapter_searching/binary_search.ts b/codes/typescript/chapter_searching/binary_search.ts index 74a87ff6..d06d164a 100644 --- a/codes/typescript/chapter_searching/binary_search.ts +++ b/codes/typescript/chapter_searching/binary_search.ts @@ -52,3 +52,5 @@ console.info('目标元素 6 的索引 = %d', index); /* 二分查找(左闭右开) */ index = binarySearch1(nums, target); console.info('目标元素 6 的索引 = %d', index); + +export {}; diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index 7f93d717..c840bfa6 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -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, 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 {}; diff --git a/codes/typescript/chapter_searching/linear_search.ts b/codes/typescript/chapter_searching/linear_search.ts index f0de33f7..530845c1 100644 --- a/codes/typescript/chapter_searching/linear_search.ts +++ b/codes/typescript/chapter_searching/linear_search.ts @@ -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 {}; diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts index cd4bf909..814ea251 100644 --- a/codes/typescript/chapter_stack_and_queue/array_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -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 {}; diff --git a/codes/typescript/chapter_stack_and_queue/array_stack.ts b/codes/typescript/chapter_stack_and_queue/array_stack.ts index 2b9f8468..cc361457 100644 --- a/codes/typescript/chapter_stack_and_queue/array_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/array_stack.ts @@ -79,4 +79,4 @@ console.log("栈的长度 size = " + size); const empty = stack.empty(); console.log("栈是否为空 = " + empty); -export { }; +export {}; diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts index 3c875929..9a412d57 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts @@ -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 {}; diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts index 1df4b499..3201cc15 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts @@ -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 {}; diff --git a/codes/typescript/chapter_stack_and_queue/queue.ts b/codes/typescript/chapter_stack_and_queue/queue.ts index 0b7444f6..7f514236 100644 --- a/codes/typescript/chapter_stack_and_queue/queue.ts +++ b/codes/typescript/chapter_stack_and_queue/queue.ts @@ -28,4 +28,4 @@ const size = queue.length; /* 判断队列是否为空 */ const empty = queue.length === 0; -export { }; +export {}; diff --git a/codes/typescript/chapter_stack_and_queue/stack.ts b/codes/typescript/chapter_stack_and_queue/stack.ts index a6d2a518..a33f2d2b 100644 --- a/codes/typescript/chapter_stack_and_queue/stack.ts +++ b/codes/typescript/chapter_stack_and_queue/stack.ts @@ -27,4 +27,4 @@ const size = stack.length; /* 判断是否为空 */ const is_empty = stack.length === 0; -export { }; \ No newline at end of file +export {}; diff --git a/codes/typescript/chapter_tree/binary_tree.ts b/codes/typescript/chapter_tree/binary_tree.ts index c7e16bd7..715267cb 100644 --- a/codes/typescript/chapter_tree/binary_tree.ts +++ b/codes/typescript/chapter_tree/binary_tree.ts @@ -33,3 +33,5 @@ printTree(n1); n1.left = n2; console.log('\n删除结点 P 后\n'); printTree(n1); + +export {}; diff --git a/codes/typescript/chapter_tree/binary_tree_bfs.ts b/codes/typescript/chapter_tree/binary_tree_bfs.ts index 7ec4a2a5..58529c2e 100644 --- a/codes/typescript/chapter_tree/binary_tree_bfs.ts +++ b/codes/typescript/chapter_tree/binary_tree_bfs.ts @@ -37,3 +37,5 @@ printTree(root); /* 层序遍历 */ const list = hierOrder(root); console.log('\n层序遍历的结点打印序列 = ' + list); + +export {}; diff --git a/codes/typescript/chapter_tree/binary_tree_dfs.ts b/codes/typescript/chapter_tree/binary_tree_dfs.ts index a61855af..759923ba 100644 --- a/codes/typescript/chapter_tree/binary_tree_dfs.ts +++ b/codes/typescript/chapter_tree/binary_tree_dfs.ts @@ -65,3 +65,5 @@ console.log('\n中序遍历的结点打印序列 = ' + list); list.length = 0; postOrder(root); console.log('\n后序遍历的结点打印序列 = ' + list); + +export {}; diff --git a/codes/typescript/module/ListNode.ts b/codes/typescript/module/ListNode.ts index fae30d48..a1e6cf6e 100644 --- a/codes/typescript/module/ListNode.ts +++ b/codes/typescript/module/ListNode.ts @@ -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 }; diff --git a/codes/typescript/module/PrintUtil.ts b/codes/typescript/module/PrintUtil.ts index 882e12fc..8da04063 100644 --- a/codes/typescript/module/PrintUtil.ts +++ b/codes/typescript/module/PrintUtil.ts @@ -4,7 +4,7 @@ * Author: Justin (xiefahit@gmail.com) */ -import ListNode from './ListNode'; +import { ListNode } from './ListNode'; import { TreeNode } from './TreeNode'; /** diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json new file mode 100644 index 00000000..b2cac7bd --- /dev/null +++ b/codes/typescript/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "CommonJS", + "outDir": "out", + "sourceMap": true + } +} \ No newline at end of file