主要是 JavaScript 在 LeetCode 上面的 ListNode
, TreeNode
, Interval
, Employee
, 方便各位调试
const node = new ListNode(val);
通过数组创建链表(遵循 LeetCode 题目规范): ListNode.create(arr : Array) : ListNode
const head = ListNode.create([1,2,3,4,5]);
ListNode.prototype.visualShow() : void
const head = ListNode.create([1,2,3,4,5]);
head.visualShow()
//1 -> 2 -> 3 -> 4 -> 5
通过数组创建交叉链表(遵循 LeetCode 题目规范, 如 160 题): ListNode.createIntersectList(firstArr: Array<any>,secondArr: Array<any>IntersectArr: Array<any>) : [ListNode, ListNode]
, 参数依次为: 首个链表未交叉部分, 下个链表未交叉部分, 交叉部分
const [head1, head2] = ListNode.createIntersectList([1,2],[3,4],[5,6]);
将链表转化为数组进行输出: ListNode.prototype.show() : Array
const head = ListNode.create([1,2,3]);
head.show();
// [1,2,3]
ListNode.show(head);
// [1,2,3]
获取链表最后一个ListNode
: ListNode.prototype.getLast() : ListNode
const head = ListNode.create([1,2,3]);
head.getLast().show();
// [3]
随机构建一个长度为 n 的链表: ListNode.mock(n : Number) : ListNode
const head = ListNode.mock(5);
Interval
const interval = new Interval(1,3);
通过数组创建区间数组(遵循 LeetCode 题目规范): Interval.createList(arr : Array[]) : Interval[]
const intervals = Interval.createList([[1,2],[3,4]])
将链表转化为数组进行输出: Interval.show() : Array
const interval = new Interval(1,3);
interval.show()
// [1,3]
Interval.show(interval)
// [1,3]
将链表数组转化为数组进行输出: Interval.showList() : Array[]
const intervals = Interval.createList([[1,2],[3,4]])
Interval.showList(intervals)
// [[1,2],[3,4]]
Employee
const employee = new Employee(1)
将 LeetCode 的 Employee 样例转换为 Employee 数组(遵循 LeetCode 题目规范): Employee.createArr(arr: [number, number, number[]][]) : Employee[]
TreeNode
const node = new TreeNode(5)
通过数组创建二叉树(遵循 LeetCode 题目规范): TreeNode.create(arr : Array) : TreeNode
注: LeetCode题目规范为单层从左至右排成数组, 不可缺少位置填
null
const head = TreeNode.create([1,2,3,null,4,null,null,5,6]);
将二叉树转化为数组进行输出: TreeNode.prototype.show() : Array
const head = TreeNode.create([1,2,3,null,4,null,null,5,6]);
head.show()
// [1,2,3,null,4,null,null,5,6]
TreeNode.show(head)
// [1,2,3,null,4,null,null,5,6]
将二叉树转化为数组进行输出: TreeNode.prototype.visualShow() : void
const head = TreeNode.create([1,2,3,null,4,null,null,5,6]);
head.visualShow()
// ---- 上为左, 下为右 ----
// 1 -> 2
// ↘→ 4 -> 5
// ↘→ 6
// ↘→ 3
Heap
const heap = new Heap([2,1,0,3,4],null,false)
第一个参数表示初始化元素数组
第二个参数表示如何从元素中取出比较值, 默认为null
, 即取出元素本身
For example:
const heap = new Heap([{val: 2, name: 'b'},{val: 1, name: 'a'}], element => element.val, false)
第三个参数表示是否是最大堆, 默认是最小堆
Heap.prototype.add(element: T): number
heap.add(5)
Heap.prototype.delete() : T
head.delete()
TreeNode.prototype.value[0]
head.value[0]
例如
1172
题, 餐盘栈. 欢迎提issue
将输入参数按照一定顺序执行
function runScript(commonds: String[], inputs: any[][], classes?: any[]): any[]
// 从左到右的参数为
// commonds 执行的命令列表
// inputs 所对应执行的参数
// classes 对应执行类数组(毕竟跨作用域了)
runScript(["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"], [[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]], [DinnerPlates])
// 详见example示例