chore: 修改eslintrc
This commit is contained in:
parent
12964047ab
commit
be5a4b4e99
@ -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 内的包
|
||||
|
@ -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)));
|
||||
|
@ -1,7 +1,5 @@
|
||||
/* leetcode 371
|
||||
给你两个整数 a 和 b ,不使用 运算符 + 和 - ,计算并返回两整数之和。
|
||||
|
||||
|
||||
给你两个整数 a 和 b ,不使用 运算符 + 和 - 计算并返回两整数之和。
|
||||
|
||||
示例 1:
|
||||
|
||||
@ -12,7 +10,6 @@
|
||||
输入:a = 2, b = 3
|
||||
输出:5
|
||||
|
||||
|
||||
提示:
|
||||
|
||||
-1000 <= a, b <= 1000
|
||||
@ -34,21 +31,22 @@
|
||||
补码:+5 的补码与其原码相同,即 00000101,-5 的补码是其反码加1得到的结果,即 11111011。
|
||||
*/
|
||||
|
||||
var getSum = function(a, b) {
|
||||
while (b != 0) {
|
||||
const getSum = (a, b) => {
|
||||
while (b !== 0) {
|
||||
const carry = (a & b) << 1;
|
||||
a = a ^ b;
|
||||
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 '';
|
||||
};
|
||||
|
||||
/**
|
||||
思考:
|
||||
@ -64,7 +62,5 @@ var getSum2 = (a, b)=>{
|
||||
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));
|
||||
|
@ -7,8 +7,8 @@ export class LinkList {
|
||||
*
|
||||
* @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('->');
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,8 +53,8 @@ export class LinkListNode {
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user