chore: 修改eslintrc
This commit is contained in:
parent
12964047ab
commit
be5a4b4e99
@ -9,11 +9,14 @@
|
|||||||
"ecmaVersion": "latest",
|
"ecmaVersion": "latest",
|
||||||
"sourceType": "module"
|
"sourceType": "module"
|
||||||
},
|
},
|
||||||
"ignores": ["leetcode/*"],
|
// "ignores": ["leetcode/*"],
|
||||||
"rules": {
|
"rules": {
|
||||||
"no-plusplus": "off", // 允许i++,i--
|
"no-plusplus": "off", // 允许i++,i--
|
||||||
"no-use-before-define": "off", // 无需在调用前面定义,之后定义也可以
|
"no-use-before-define": "off", // 无需在调用前面定义,之后定义也可以
|
||||||
"no-console": "off",
|
"no-console": "off",
|
||||||
|
"no-continue": "off", // 允许使用continue
|
||||||
|
"no-bitwise": "off", // 允许使用位运算
|
||||||
|
"max-classes-per-file": "off", // 允许一个文件写多个类
|
||||||
"import/extensions": [
|
"import/extensions": [
|
||||||
"error",
|
"error",
|
||||||
"ignorePackages", // 忽略 node_modules 内的包
|
"ignorePackages", // 忽略 node_modules 内的包
|
||||||
|
@ -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]
|
// 输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
|
||||||
// 输出:[8,9,9,9,0,0,0,1]
|
// 输出:[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
|
function addTwoNumbers(l1, l2) {
|
||||||
const l2 = LinkList.fromArray([5,6,4]).head
|
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(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)));
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
/* leetcode 371
|
/* leetcode 371
|
||||||
给你两个整数 a 和 b ,不使用 运算符 + 和 - ,计算并返回两整数之和。
|
给你两个整数 a 和 b ,不使用 运算符 + 和 - 计算并返回两整数之和。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
示例 1:
|
示例 1:
|
||||||
|
|
||||||
@ -11,7 +9,6 @@
|
|||||||
|
|
||||||
输入:a = 2, b = 3
|
输入:a = 2, b = 3
|
||||||
输出:5
|
输出:5
|
||||||
|
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
|
|
||||||
@ -34,21 +31,22 @@
|
|||||||
补码:+5 的补码与其原码相同,即 00000101,-5 的补码是其反码加1得到的结果,即 11111011。
|
补码:+5 的补码与其原码相同,即 00000101,-5 的补码是其反码加1得到的结果,即 11111011。
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var getSum = function(a, b) {
|
const getSum = (a, b) => {
|
||||||
while (b != 0) {
|
while (b !== 0) {
|
||||||
const carry = (a & b) << 1;
|
const carry = (a & b) << 1;
|
||||||
a = a ^ b;
|
a ^= b;
|
||||||
b = carry;
|
b = carry;
|
||||||
}
|
}
|
||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
|
|
||||||
var getSum2 = (a, b)=>{
|
const getSum2 = (a, b) => {
|
||||||
if(b===0) return a // 如果进位结果为0就结束递归
|
if (b === 0) return a; // 如果进位结果为0就结束递归
|
||||||
const carry = (a & b) << 1;
|
const carry = (a & b) << 1;
|
||||||
const c = a ^ b;
|
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
|
4 5 6 4 5 6
|
||||||
6 4 5 6 4 5
|
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));
|
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
*/
|
*/
|
||||||
export class LinkList {
|
export class LinkList {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {LinkListNode} head - 链表的头节点
|
* @param {LinkListNode} head - 链表的头节点
|
||||||
*/
|
*/
|
||||||
constructor(head){
|
constructor(head) {
|
||||||
this.head = head
|
this.head = head;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,29 +16,29 @@ export class LinkList {
|
|||||||
* @param {any[]} array - 要转换的数组
|
* @param {any[]} array - 要转换的数组
|
||||||
* @returns { LinkList } 按数组顺序返回的链表
|
* @returns { LinkList } 按数组顺序返回的链表
|
||||||
*/
|
*/
|
||||||
static fromArray(array){
|
static fromArray(array) {
|
||||||
let n = array.length
|
const n = array.length;
|
||||||
const head = new LinkListNode(array[0])
|
const head = new LinkListNode(array[0]);
|
||||||
const linkList = new LinkList(head)
|
const linkList = new LinkList(head);
|
||||||
let curNode = head
|
let curNode = head;
|
||||||
for(let i=1;i<n;i++){
|
for (let i = 1; i < n; i++) {
|
||||||
curNode.next = new LinkListNode(array[i])
|
curNode.next = new LinkListNode(array[i]);
|
||||||
curNode = curNode.next
|
curNode = curNode.next;
|
||||||
}
|
}
|
||||||
return linkList
|
return linkList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 返回序列化之后的链表 1->2->3
|
* @description 返回序列化之后的链表 1->2->3
|
||||||
*/
|
*/
|
||||||
toString(){
|
toString() {
|
||||||
let cur = this.head || this // 兼容节点自身遍历
|
let cur = this.head || this; // 兼容节点自身遍历
|
||||||
let tmpArr = []
|
const tmpArr = [];
|
||||||
while(cur){
|
while (cur) {
|
||||||
tmpArr.push(cur.val)
|
tmpArr.push(cur.val);
|
||||||
cur = cur.next
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
return tmpArr.join('->')
|
return tmpArr.join('->');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,13 +48,13 @@ export class LinkList {
|
|||||||
*/
|
*/
|
||||||
export class LinkListNode {
|
export class LinkListNode {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @description 创建一个节点实例
|
* @description 创建一个节点实例
|
||||||
* @param {any} val - 当前节点保存的值
|
* @param {any} val - 当前节点保存的值
|
||||||
* @param {LinkListNode} next - 当前节点下一个节点,默认为空
|
* @param {LinkListNode} next - 当前节点下一个节点,默认为空
|
||||||
*/
|
*/
|
||||||
constructor(val, next = null){
|
constructor(val, next = null) {
|
||||||
this.val = val
|
this.val = val;
|
||||||
this.next = next
|
this.next = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user