Update TypeScript codes.
This commit is contained in:
parent
7d14c9440e
commit
29dbe8cd82
4
codes/typescript/.gitignore
vendored
Normal file
4
codes/typescript/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
node_modules
|
||||||
|
out
|
||||||
|
package.json
|
||||||
|
package-lock.json
|
@ -4,7 +4,7 @@
|
|||||||
* Author: Justin (xiefahit@gmail.com)
|
* Author: Justin (xiefahit@gmail.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import ListNode from '../module/ListNode';
|
import { ListNode } from '../module/ListNode';
|
||||||
import { printLinkedList } from '../module/PrintUtil';
|
import { printLinkedList } from '../module/PrintUtil';
|
||||||
|
|
||||||
/* 在链表的结点 n0 之后插入结点 P */
|
/* 在链表的结点 n0 之后插入结点 P */
|
||||||
|
@ -42,3 +42,5 @@ console.log("方法一 res = ", res);
|
|||||||
// 方法二
|
// 方法二
|
||||||
res = twoSumHashTable(nums, target);
|
res = twoSumHashTable(nums, target);
|
||||||
console.log("方法二 res = ", res);
|
console.log("方法二 res = ", res);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -153,3 +153,5 @@ console.log("线性对数阶(递归实现)的计算操作数量 = " + count)
|
|||||||
|
|
||||||
count = factorialRecur(n);
|
count = factorialRecur(n);
|
||||||
console.log("阶乘阶(递归实现)的计算操作数量 = " + count);
|
console.log("阶乘阶(递归实现)的计算操作数量 = " + count);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -32,8 +32,7 @@ function findOne(nums: number[]): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
function main(): void {
|
for (let i = 0; i < 10; i++) {
|
||||||
for (let i = 0; i < 10; i++) {
|
|
||||||
let n = 100;
|
let n = 100;
|
||||||
let nums = randomNumbers(n);
|
let nums = randomNumbers(n);
|
||||||
let index = findOne(nums);
|
let index = findOne(nums);
|
||||||
@ -41,5 +40,6 @@ function main(): void {
|
|||||||
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
|
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
|
||||||
);
|
);
|
||||||
console.log("数字 1 的索引为 " + index);
|
console.log("数字 1 的索引为 " + index);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -42,3 +42,5 @@ console.info('\n单独遍历值 Value');
|
|||||||
for (const v of map.values()) {
|
for (const v of map.values()) {
|
||||||
console.info(v);
|
console.info(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -52,3 +52,5 @@ console.info('目标元素 6 的索引 = %d', index);
|
|||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearch1(nums, target);
|
||||||
console.info('目标元素 6 的索引 = %d', index);
|
console.info('目标元素 6 的索引 = %d', index);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { printLinkedList } from "../module/PrintUtil";
|
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;
|
return map.has(target) ? map.get(target) as ListNode : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
|
||||||
const target = 3;
|
|
||||||
|
|
||||||
/* 哈希查找(数组) */
|
/* Driver Code */
|
||||||
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
const target = 3;
|
||||||
// 初始化哈希表
|
|
||||||
const map = new Map();
|
/* 哈希查找(数组) */
|
||||||
for (let i = 0; i < nums.length; i++) {
|
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: 索引
|
map.set(nums[i], i); // key: 元素,value: 索引
|
||||||
}
|
}
|
||||||
const index = hashingSearch(map, target);
|
const index = hashingSearch(map, target);
|
||||||
console.log("目标元素 3 的索引 = " + index);
|
console.log("目标元素 3 的索引 = " + index);
|
||||||
|
|
||||||
/* 哈希查找(链表) */
|
/* 哈希查找(链表) */
|
||||||
let head = new ListNode().arrToLinkedList(nums)
|
let head = arrToLinkedList(nums)
|
||||||
// 初始化哈希表
|
// 初始化哈希表
|
||||||
const map1 = new Map();
|
const map1 = new Map();
|
||||||
while (head != null) {
|
while (head != null) {
|
||||||
map1.set(head.val, head); // key: 结点值,value: 结点
|
map1.set(head.val, head); // key: 结点值,value: 结点
|
||||||
head = head.next;
|
head = head.next;
|
||||||
}
|
|
||||||
const node = hashingSearch1(map1, target);
|
|
||||||
console.log("目标结点值 3 的对应结点对象为");
|
|
||||||
printLinkedList(node);
|
|
||||||
}
|
}
|
||||||
|
const node = hashingSearch1(map1, target);
|
||||||
|
console.log("目标结点值 3 的对应结点对象为", node);
|
||||||
|
|
||||||
main();
|
export {};
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* Author: Daniel (better.sunjian@gmail.com)
|
* 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 {
|
function linearSearchArray(nums: number[], target: number): number {
|
||||||
@ -42,6 +42,8 @@ const index = linearSearchArray(nums, target);
|
|||||||
console.log('目标元素 3 的索引 =', index);
|
console.log('目标元素 3 的索引 =', index);
|
||||||
|
|
||||||
/* 在链表中执行线性查找 */
|
/* 在链表中执行线性查找 */
|
||||||
const head = ListNode.arrToLinkedList(nums);
|
const head = arrToLinkedList(nums);
|
||||||
const node = linearSearchLinkedList(head, target);
|
const node = linearSearchLinkedList(head, target);
|
||||||
console.log('目标结点值 3 的对应结点对象为', node);
|
console.log('目标结点值 3 的对应结点对象为', node);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -102,7 +102,6 @@ for (let i = 0; i < 10; i++) {
|
|||||||
queue.push(i);
|
queue.push(i);
|
||||||
queue.poll();
|
queue.poll();
|
||||||
console.log("第 " + i + " 轮入队 + 出队后 queue =", queue.toArray());
|
console.log("第 " + i + " 轮入队 + 出队后 queue =", queue.toArray());
|
||||||
console.log(queue.toArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { };
|
export {};
|
||||||
|
@ -79,4 +79,4 @@ console.log("栈的长度 size = " + size);
|
|||||||
const empty = stack.empty();
|
const empty = stack.empty();
|
||||||
console.log("栈是否为空 = " + empty);
|
console.log("栈是否为空 = " + empty);
|
||||||
|
|
||||||
export { };
|
export {};
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import ListNode from "../module/ListNode"
|
import { ListNode } from "../module/ListNode"
|
||||||
|
|
||||||
/* 基于链表实现的队列 */
|
/* 基于链表实现的队列 */
|
||||||
class LinkedListQueue {
|
class LinkedListQueue {
|
||||||
@ -100,3 +100,5 @@ console.log("队列长度 size = " + size);
|
|||||||
/* 判断队列是否为空 */
|
/* 判断队列是否为空 */
|
||||||
const isEmpty = queue.isEmpty();
|
const isEmpty = queue.isEmpty();
|
||||||
console.log("队列是否为空 = " + isEmpty);
|
console.log("队列是否为空 = " + isEmpty);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import ListNode from "../module/ListNode"
|
import { ListNode } from "../module/ListNode"
|
||||||
|
|
||||||
/* 基于链表实现的栈 */
|
/* 基于链表实现的栈 */
|
||||||
class LinkedListStack {
|
class LinkedListStack {
|
||||||
@ -89,3 +89,5 @@ console.log("栈的长度 size = " + size);
|
|||||||
/* 判断是否为空 */
|
/* 判断是否为空 */
|
||||||
const isEmpty = stack.isEmpty();
|
const isEmpty = stack.isEmpty();
|
||||||
console.log("栈是否为空 = " + isEmpty);
|
console.log("栈是否为空 = " + isEmpty);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -28,4 +28,4 @@ const size = queue.length;
|
|||||||
/* 判断队列是否为空 */
|
/* 判断队列是否为空 */
|
||||||
const empty = queue.length === 0;
|
const empty = queue.length === 0;
|
||||||
|
|
||||||
export { };
|
export {};
|
||||||
|
@ -27,4 +27,4 @@ const size = stack.length;
|
|||||||
/* 判断是否为空 */
|
/* 判断是否为空 */
|
||||||
const is_empty = stack.length === 0;
|
const is_empty = stack.length === 0;
|
||||||
|
|
||||||
export { };
|
export {};
|
||||||
|
@ -33,3 +33,5 @@ printTree(n1);
|
|||||||
n1.left = n2;
|
n1.left = n2;
|
||||||
console.log('\n删除结点 P 后\n');
|
console.log('\n删除结点 P 后\n');
|
||||||
printTree(n1);
|
printTree(n1);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -37,3 +37,5 @@ printTree(root);
|
|||||||
/* 层序遍历 */
|
/* 层序遍历 */
|
||||||
const list = hierOrder(root);
|
const list = hierOrder(root);
|
||||||
console.log('\n层序遍历的结点打印序列 = ' + list);
|
console.log('\n层序遍历的结点打印序列 = ' + list);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -65,3 +65,5 @@ console.log('\n中序遍历的结点打印序列 = ' + list);
|
|||||||
list.length = 0;
|
list.length = 0;
|
||||||
postOrder(root);
|
postOrder(root);
|
||||||
console.log('\n后序遍历的结点打印序列 = ' + list);
|
console.log('\n后序遍历的结点打印序列 = ' + list);
|
||||||
|
|
||||||
|
export {};
|
||||||
|
@ -7,20 +7,21 @@
|
|||||||
/**
|
/**
|
||||||
* Definition for a singly-linked list node
|
* Definition for a singly-linked list node
|
||||||
*/
|
*/
|
||||||
export default class ListNode {
|
class ListNode {
|
||||||
val: number;
|
val: number;
|
||||||
next: ListNode | null;
|
next: ListNode | null;
|
||||||
constructor(val?: number, next?: ListNode | null) {
|
constructor(val?: number, next?: ListNode | null) {
|
||||||
this.val = val === undefined ? 0 : val;
|
this.val = val === undefined ? 0 : val;
|
||||||
this.next = next === undefined ? null : next;
|
this.next = next === undefined ? null : next;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a linked list with an array
|
* Generate a linked list with an array
|
||||||
* @param arr
|
* @param arr
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
arrToLinkedList(arr: number[]): ListNode | null {
|
function arrToLinkedList(arr: number[]): ListNode | null {
|
||||||
const dum: ListNode = new ListNode(0);
|
const dum: ListNode = new ListNode(0);
|
||||||
let head = dum;
|
let head = dum;
|
||||||
for (const val of arr) {
|
for (const val of arr) {
|
||||||
@ -28,5 +29,6 @@ export default class ListNode {
|
|||||||
head = head.next;
|
head = head.next;
|
||||||
}
|
}
|
||||||
return dum.next;
|
return dum.next;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { ListNode, arrToLinkedList };
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* Author: Justin (xiefahit@gmail.com)
|
* Author: Justin (xiefahit@gmail.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import ListNode from './ListNode';
|
import { ListNode } from './ListNode';
|
||||||
import { TreeNode } from './TreeNode';
|
import { TreeNode } from './TreeNode';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
8
codes/typescript/tsconfig.json
Normal file
8
codes/typescript/tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES6",
|
||||||
|
"module": "CommonJS",
|
||||||
|
"outDir": "out",
|
||||||
|
"sourceMap": true
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user