mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Fomrat the JS and TS codes with prettier.
This commit is contained in:
@ -69,29 +69,29 @@ function find(nums, target) {
|
||||
/* Driver Codes*/
|
||||
/* 初始化数组 */
|
||||
const arr = new Array(5).fill(0);
|
||||
console.log("数组 arr =", arr);
|
||||
console.log('数组 arr =', arr);
|
||||
let nums = [1, 3, 2, 5, 4];
|
||||
console.log("数组 nums =", nums);
|
||||
console.log('数组 nums =', nums);
|
||||
|
||||
/* 随机访问 */
|
||||
let random_num = randomAccess(nums);
|
||||
console.log("在 nums 中获取随机元素", random_num);
|
||||
console.log('在 nums 中获取随机元素', random_num);
|
||||
|
||||
/* 长度扩展 */
|
||||
nums = extend(nums, 3);
|
||||
console.log("将数组长度扩展至 8 ,得到 nums =", nums);
|
||||
console.log('将数组长度扩展至 8 ,得到 nums =', nums);
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, 6, 3);
|
||||
console.log("在索引 3 处插入数字 6 ,得到 nums =", nums);
|
||||
console.log('在索引 3 处插入数字 6 ,得到 nums =', nums);
|
||||
|
||||
/* 删除元素 */
|
||||
remove(nums, 2);
|
||||
console.log("删除索引 2 处的元素,得到 nums =", nums);
|
||||
console.log('删除索引 2 处的元素,得到 nums =', nums);
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(nums);
|
||||
|
||||
/* 查找元素 */
|
||||
let index = find(nums, 3);
|
||||
console.log("在 nums 中查找元素 3 ,得到索引 =", index);
|
||||
console.log('在 nums 中查找元素 3 ,得到索引 =', index);
|
||||
|
||||
@ -4,8 +4,8 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com), Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { printLinkedList } = require("../modules/PrintUtil");
|
||||
const { ListNode } = require("../modules/ListNode");
|
||||
const { printLinkedList } = require('../modules/PrintUtil');
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
function insert(n0, P) {
|
||||
@ -16,8 +16,7 @@ function insert(n0, P) {
|
||||
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
function remove(n0) {
|
||||
if (!n0.next)
|
||||
return;
|
||||
if (!n0.next) return;
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
@ -61,23 +60,23 @@ n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
console.log("初始化的链表为");
|
||||
console.log('初始化的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 插入节点 */
|
||||
insert(n0, new ListNode(0));
|
||||
console.log("插入节点后的链表为");
|
||||
console.log('插入节点后的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 删除节点 */
|
||||
remove(n0);
|
||||
console.log("删除节点后的链表为");
|
||||
console.log('删除节点后的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 访问节点 */
|
||||
const node = access(n0, 3);
|
||||
console.log("链表中索引 3 处的节点的值 = " + node.val);
|
||||
console.log('链表中索引 3 处的节点的值 = ' + node.val);
|
||||
|
||||
/* 查找节点 */
|
||||
const index = find(n0, 2);
|
||||
console.log("链表中值为 2 的节点的索引 = " + index);
|
||||
console.log('链表中值为 2 的节点的索引 = ' + index);
|
||||
|
||||
@ -29,15 +29,13 @@ class MyList {
|
||||
/* 访问元素 */
|
||||
get(index) {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
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('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
this.#nums[index] = num;
|
||||
}
|
||||
|
||||
@ -54,8 +52,7 @@ class MyList {
|
||||
|
||||
/* 中间插入元素 */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
@ -71,8 +68,7 @@ class MyList {
|
||||
|
||||
/* 删除元素 */
|
||||
remove(index) {
|
||||
if (index < 0 || index >= this.#size)
|
||||
throw new Error('索引越界');
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
let num = this.#nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (let j = index; j < this.#size - 1; j++) {
|
||||
|
||||
Reference in New Issue
Block a user