Add build script for JS and TS codes.

This commit is contained in:
krahets
2023-02-08 19:45:06 +08:00
parent 22b7d65d20
commit 05f0054005
28 changed files with 227 additions and 2217 deletions

View File

@ -148,27 +148,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "JavaScript"
```javascript title="array.js"
/* 随机返回一个数组元素 */
function randomAccess(nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
const random_index = Math.floor(Math.random() * nums.length);
// 获取并返回随机元素
const random_num = nums[random_index];
return random_num;
}
[class]{}-[func]{randomAccess}
```
=== "TypeScript"
```typescript title="array.ts"
/* 随机返回一个数组元素 */
function randomAccess(nums: number[]): number {
// 在区间 [0, nums.length) 中随机抽取一个数字
const random_index = Math.floor(Math.random() * nums.length);
// 获取并返回随机元素
const random_num = nums[random_index];
return random_num;
}
[class]{}-[func]{randomAccess}
```
=== "C"
@ -259,33 +245,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "JavaScript"
```javascript title="array.js"
/* 扩展数组长度 */
function extend(nums, enlarge) {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
// 将原数组中的所有元素复制到新数组
for (let i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
return res;
}
[class]{}-[func]{extend}
```
=== "TypeScript"
```typescript title="array.ts"
/* 扩展数组长度 */
function extend(nums: number[], enlarge: number): number[] {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
// 将原数组中的所有元素复制到新数组
for (let i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
return res;
}
[class]{}-[func]{extend}
```
=== "C"
@ -402,45 +368,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "JavaScript"
```javascript title="array.js"
/* 在数组的索引 index 处插入元素 num */
function insert(nums, num, index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (let i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
function remove(nums, index) {
// 把索引 index 之后的所有元素向前移动一位
for (let i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
[class]{}-[func]{insert}
[class]{}-[func]{remove}
```
=== "TypeScript"
```typescript title="array.ts"
/* 在数组的索引 index 处插入元素 num */
function insert(nums: number[], num: number, index: number): void {
// 把索引 index 以及之后的所有元素向后移动一位
for (let i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
function remove(nums: number[], index: number): void {
// 把索引 index 之后的所有元素向前移动一位
for (let i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
[class]{}-[func]{insert}
[class]{}-[func]{remove}
```
=== "C"
@ -563,35 +501,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "JavaScript"
```javascript title="array.js"
/* 遍历数组 */
function traverse(nums) {
let count = 0;
// 通过索引遍历数组
for (let i = 0; i < nums.length; i++) {
count++;
}
// 直接遍历数组
for (let num of nums) {
count += 1;
}
}
[class]{}-[func]{traverse}
```
=== "TypeScript"
```typescript title="array.ts"
/* 遍历数组 */
function traverse(nums: number[]): void {
let count = 0;
// 通过索引遍历数组
for (let i = 0; i < nums.length; i++) {
count++;
}
// 直接遍历数组
for(let num of nums){
count += 1;
}
}
[class]{}-[func]{traverse}
```
=== "C"
@ -695,27 +611,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "JavaScript"
```javascript title="array.js"
/* 在数组中查找指定元素 */
function find(nums, target) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] == target) return i;
}
return -1;
}
[class]{}-[func]{find}
```
=== "TypeScript"
```typescript title="array.ts"
/* 在数组中查找指定元素 */
function find(nums: number[], target: number): number {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === target) {
return i;
}
}
return -1;
}
[class]{}-[func]{find}
```
=== "C"

View File

@ -538,30 +538,13 @@ comments: true
=== "JavaScript"
```javascript title="linked_list.js"
/* 访问链表中索引为 index 的结点 */
function access(head, index) {
for (let i = 0; i < index; i++) {
if (!head)
return null;
head = head.next;
}
return head;
}
[class]{}-[func]{access}
```
=== "TypeScript"
```typescript title="linked_list.ts"
/* 访问链表中索引为 index 的结点 */
function access(head: ListNode | null, index: number): ListNode | null {
for (let i = 0; i < index; i++) {
if (!head) {
return null;
}
head = head.next;
}
return head;
}
[class]{}-[func]{access}
```
=== "C"
@ -661,35 +644,13 @@ comments: true
=== "JavaScript"
```javascript title="linked_list.js"
/* 在链表中查找值为 target 的首个结点 */
function find(head, target) {
let index = 0;
while (head !== null) {
if (head.val === target) {
return index;
}
head = head.next;
index += 1;
}
return -1;
}
[class]{}-[func]{find}
```
=== "TypeScript"
```typescript title="linked_list.ts"
/* 在链表中查找值为 target 的首个结点 */
function find(head: ListNode | null, target: number): number {
let index = 0;
while (head !== null) {
if (head.val === target) {
return index;
}
head = head.next;
index += 1;
}
return -1;
}
[class]{}-[func]{find}
```
=== "C"

View File

@ -836,190 +836,13 @@ comments: true
=== "JavaScript"
```javascript title="my_list.js"
/* 列表类简易实现 */
class MyList {
#nums = new Array(); // 数组(存储列表元素)
#capacity = 10; // 列表容量
#size = 0; // 列表长度(即当前元素数量)
#extendRatio = 2; // 每次列表扩容的倍数
/* 构造函数 */
constructor() {
this.#nums = new Array(this.#capacity);
}
/* 获取列表长度(即当前元素数量)*/
size() {
return this.#size;
}
/* 获取列表容量 */
capacity() {
return this.#capacity;
}
/* 访问元素 */
get(index) {
// 索引如果越界则抛出异常,下同
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('索引越界');
this.#nums[index] = num;
}
/* 尾部添加元素 */
add(num) {
// 如果长度等于容量,则需要扩容
if (this.#size === this.#capacity) {
this.extendCapacity();
}
// 将新元素添加到列表尾部
this.#nums[this.#size] = num;
this.#size++;
}
/* 中间插入元素 */
insert(index, num) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
// 元素数量超出容量时,触发扩容机制
if (this.#size === this.#capacity) {
this.extendCapacity();
}
// 将索引 index 以及之后的元素都向后移动一位
for (let j = this.#size - 1; j >= index; j--) {
this.#nums[j + 1] = this.#nums[j];
}
// 更新元素数量
this.#nums[index] = num;
this.#size++;
}
/* 删除元素 */
remove(index) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
let num = this.#nums[index];
// 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this.#size - 1; j++) {
this.#nums[j] = this.#nums[j + 1];
}
// 更新元素数量
this.#size--;
// 返回被删除元素
return num;
}
/* 列表扩容 */
extendCapacity() {
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
this.#nums = this.#nums.concat(
new Array(this.capacity() * (this.#extendRatio - 1))
);
// 更新列表容量
this.#capacity = this.#nums.length;
}
}
[class]{MyList}-[func]{}
```
=== "TypeScript"
```typescript title="my_list.ts"
/* 列表类简易实现 */
class MyList {
private nums: Array<number>; // 数组(存储列表元素)
private _capacity: number = 10; // 列表容量
private _size: number = 0; // 列表长度(即当前元素数量)
private extendRatio: number = 2; // 每次列表扩容的倍数
/* 构造函数 */
constructor() {
this.nums = new Array(this._capacity);
}
/* 获取列表长度(即当前元素数量)*/
public size(): number {
return this._size;
}
/* 获取列表容量 */
public capacity(): number {
return this._capacity;
}
/* 访问元素 */
public get(index: number): number {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= this._size)
throw new Error('索引越界');
return this.nums[index];
}
/* 更新元素 */
public set(index: number, num: number): void {
if (index < 0 || index >= this._size)
throw new Error('索引越界');
this.nums[index] = num;
}
/* 尾部添加元素 */
public add(num: number): void {
// 如果长度等于容量,则需要扩容
if (this._size === this._capacity)
this.extendCapacity();
// 将新元素添加到列表尾部
this.nums[this._size] = num;
this._size++;
}
/* 中间插入元素 */
public insert(index: number, num: number): void {
if (index < 0 || index >= this._size)
throw new Error('索引越界');
// 元素数量超出容量时,触发扩容机制
if (this._size === this._capacity) {
this.extendCapacity();
}
// 将索引 index 以及之后的元素都向后移动一位
for (let j = this._size - 1; j >= index; j--) {
this.nums[j + 1] = this.nums[j];
}
// 更新元素数量
this.nums[index] = num;
this._size++;
}
/* 删除元素 */
public remove(index: number): number {
if (index < 0 || index >= this._size)
throw new Error('索引越界');
let num = this.nums[index];
// 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this._size - 1; j++) {
this.nums[j] = this.nums[j + 1];
}
// 更新元素数量
this._size--;
// 返回被删除元素
return num;
}
/* 列表扩容 */
public extendCapacity(): void {
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
this.nums = this.nums.concat(
new Array(this.capacity() * (this.extendRatio - 1))
);
// 更新列表容量
this._capacity = this.nums.length;
}
}
[class]{MyList}-[func]{}
```
=== "C"