From be5a4b4e999fd495fc0fe438a097fb52513a2009 Mon Sep 17 00:00:00 2001 From: = <--gbloal> Date: Sun, 19 May 2024 03:36:36 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BF=AE=E6=94=B9eslintrc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.json | 5 +- leetcode/中等/两数相加.js | 120 ++++++++++++++++++------------------ leetcode/中等/两整数之和.js | 32 +++++----- list/link-list.js | 48 +++++++-------- 4 files changed, 102 insertions(+), 103 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 97246de..8748055 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -9,11 +9,14 @@ "ecmaVersion": "latest", "sourceType": "module" }, - "ignores": ["leetcode/*"], + // "ignores": ["leetcode/*"], "rules": { "no-plusplus": "off", // 允许i++,i-- "no-use-before-define": "off", // 无需在调用前面定义,之后定义也可以 "no-console": "off", + "no-continue": "off", // 允许使用continue + "no-bitwise": "off", // 允许使用位运算 + "max-classes-per-file": "off", // 允许一个文件写多个类 "import/extensions": [ "error", "ignorePackages", // 忽略 node_modules 内的包 diff --git a/leetcode/中等/两数相加.js b/leetcode/中等/两数相加.js index 6edaa79..c615ffb 100644 --- a/leetcode/中等/两数相加.js +++ b/leetcode/中等/两数相加.js @@ -20,71 +20,71 @@ 解释:也就是用列表表示 */ -function addTwoNumbers(l1, l2) { - const head = new ListNode() - let curNode = head - let carry = 0 - while (l1!==null || l2!==null) { - let x = l1 !== null ? l1.val : 0 - let y = l2 !== null ? l2.val : 0 - let sum = x + y + carry - curNode.val = sum % 10 - carry = Math.floor(sum/10) - if(l1!==null) l1=l1.next - if(l2!==null) l2=l2.next - if(carry > 0 || l1 !=null || l2 != null){ - curNode.next = new ListNode() - curNode = curNode.next - } - } - if(carry!==0) { - curNode.val = carry - } - return head -} - -function addTwoNumbers2(l1, l2){ - const task1 = [] - const task2 = [] - while(l1 || l2) { - if(l1){ - task1.push(l1.val) - l1 = l1.next - } - - if(l2){ - task2.push(l2.val) - l2 = l2.next - } - } - let target = BigInt(task1.reverse().join('')) + BigInt(task2.reverse().join('')) - - // console.log(Number(task1.reverse().join(''))); - console.log(target); - - const head = new ListNode() - let cur = head - while (target) { - cur.val = target % 10n - target = target / 10n - cur.next = target ? new ListNode() : null - cur = cur.next - } - return head -} - // 换一种思路,我们按照顺序压入栈中,再依次弹出,之后生成对应的数,最后再把这个数转化为链表不就行了吗? - - - // 输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] // 输出:[8,9,9,9,0,0,0,1] -import {LinkList, LinkListNode as ListNode} from '../../list/index.js' +import { LinkList, LinkListNode as ListNode } from '../../list/index.js'; -const l1 = LinkList.fromArray([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]).head -const l2 = LinkList.fromArray([5,6,4]).head +function addTwoNumbers(l1, l2) { + const head = new ListNode(); + let curNode = head; + let carry = 0; + while (l1 !== null || l2 !== null) { + const x = l1 !== null ? l1.val : 0; + const y = l2 !== null ? l2.val : 0; + const sum = x + y + carry; + curNode.val = sum % 10; + carry = Math.floor(sum / 10); + if (l1 !== null) l1 = l1.next; + if (l2 !== null) l2 = l2.next; + if (carry > 0 || l1 != null || l2 != null) { + curNode.next = new ListNode(); + curNode = curNode.next; + } + } + if (carry !== 0) { + curNode.val = carry; + } + return head; +} + +function addTwoNumbers2(l1, l2) { + const task1 = []; + const task2 = []; + while (l1 || l2) { + if (l1) { + task1.push(l1.val); + l1 = l1.next; + } + + if (l2) { + task2.push(l2.val); + l2 = l2.next; + } + } + let target = BigInt(task1.reverse().join('')) + BigInt(task2.reverse().join('')); + + // console.log(Number(task1.reverse().join(''))); + console.log(target); + + const head = new ListNode(); + let cur = head; + while (target) { + cur.val = target % 10n; + target /= 10n; + cur.next = target ? new ListNode() : null; + cur = cur.next; + } + return head; +} + +const l1 = LinkList.fromArray( + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], +).head; +const l2 = LinkList.fromArray([5, 6, 4]).head; // console.log(LinkList.prototype.toString.apply(addTwoNumbers(l1,l2))); -console.log(LinkList.prototype.toString.apply(addTwoNumbers2(l1,l2))); \ No newline at end of file +console.log(LinkList.prototype.toString.apply(addTwoNumbers2(l1, l2))); +console.log(LinkList.prototype.toString.apply(addTwoNumbers(l1, l2))); diff --git a/leetcode/中等/两整数之和.js b/leetcode/中等/两整数之和.js index 302bf6d..89744ef 100644 --- a/leetcode/中等/两整数之和.js +++ b/leetcode/中等/两整数之和.js @@ -1,7 +1,5 @@ /* leetcode 371 -给你两个整数 a 和 b ,不使用 运算符 + 和 - ​​​​​​​,计算并返回两整数之和。 - - +给你两个整数 a 和 b ,不使用 运算符 + 和 - 计算并返回两整数之和。 示例 1: @@ -11,7 +9,6 @@ 输入:a = 2, b = 3 输出:5 - 提示: @@ -34,21 +31,22 @@ 补码:+5 的补码与其原码相同,即 00000101,-5 的补码是其反码加1得到的结果,即 11111011。 */ -var getSum = function(a, b) { - while (b != 0) { - const carry = (a & b) << 1; - a = a ^ b; - b = carry; +const getSum = (a, b) => { + while (b !== 0) { + const carry = (a & b) << 1; + a ^= b; + b = carry; } return a; }; -var getSum2 = (a, b)=>{ - if(b===0) return a // 如果进位结果为0就结束递归 +const getSum2 = (a, b) => { + if (b === 0) return a; // 如果进位结果为0就结束递归 const carry = (a & b) << 1; const c = a ^ b; - getSum2(c, carry) -} + getSum2(c, carry); + return ''; +}; /** 思考: @@ -61,10 +59,8 @@ var getSum2 = (a, b)=>{ 4 5 6 4 5 6 6 4 5 6 4 5 -------- --------- - 0 9 1 1 1 1 1 + 0 9 1 1 1 1 1 */ - - -console.log(getSum(1,0)); -console.log(getSum2(2,0)); +console.log(getSum(1, 0)); +console.log(getSum2(2, 0)); diff --git a/list/link-list.js b/list/link-list.js index bc2ecc9..d140296 100644 --- a/list/link-list.js +++ b/list/link-list.js @@ -4,11 +4,11 @@ */ export class LinkList { /** - * - * @param {LinkListNode} head - 链表的头节点 + * + * @param {LinkListNode} head - 链表的头节点 */ - constructor(head){ - this.head = head + constructor(head) { + this.head = head; } /** @@ -16,29 +16,29 @@ export class LinkList { * @param {any[]} array - 要转换的数组 * @returns { LinkList } 按数组顺序返回的链表 */ - static fromArray(array){ - let n = array.length - const head = new LinkListNode(array[0]) - const linkList = new LinkList(head) - let curNode = head - for(let i=1;i2->3 */ - toString(){ - let cur = this.head || this // 兼容节点自身遍历 - let tmpArr = [] - while(cur){ - tmpArr.push(cur.val) - cur = cur.next + toString() { + let cur = this.head || this; // 兼容节点自身遍历 + const tmpArr = []; + while (cur) { + tmpArr.push(cur.val); + cur = cur.next; } - return tmpArr.join('->') + return tmpArr.join('->'); } } @@ -48,13 +48,13 @@ export class LinkList { */ export class LinkListNode { /** - * + * * @description 创建一个节点实例 * @param {any} val - 当前节点保存的值 * @param {LinkListNode} next - 当前节点下一个节点,默认为空 */ - constructor(val, next = null){ - this.val = val - this.next = next + constructor(val, next = null) { + this.val = val; + this.next = next; } }