chore: 修改eslintrc

This commit is contained in:
= 2024-05-19 03:36:36 +08:00
parent 12964047ab
commit be5a4b4e99
4 changed files with 102 additions and 103 deletions

View File

@ -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

View File

@ -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)));
console.log(LinkList.prototype.toString.apply(addTwoNumbers2(l1, l2)));
console.log(LinkList.prototype.toString.apply(addTwoNumbers(l1, l2)));

View File

@ -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));

View File

@ -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;i<n;i++){
curNode.next = new LinkListNode(array[i])
curNode = curNode.next
static fromArray(array) {
const n = array.length;
const head = new LinkListNode(array[0]);
const linkList = new LinkList(head);
let curNode = head;
for (let i = 1; i < n; i++) {
curNode.next = new LinkListNode(array[i]);
curNode = curNode.next;
}
return linkList
return linkList;
}
/**
* @description 返回序列化之后的链表 1->2->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;
}
}