LeetCode & linked list bug All In One
LeetCode & linked list bug All In One
add-two-numbers
https://leetcode.com/problems/add-two-numbers/
https://leetcode.cn/problems/add-two-numbers/
// 字符串相加 / 大数相加
const addStrings = function(num1, num2) {
let res = '';
let temp = 0;
const arr1 = num1.split('');
const arr2 = num2.split('');
while (arr1.length || arr2.length || temp) {
// `~~` double bitwise not 双非位运算, 字符串转换成整数
temp += ~~arr1.pop() + ~~arr2.pop();
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9 ? 1 : 0;
}
// console.log('res =', res, typeof res);
return res === "0" ? res : res.replace(/^0+/, '');
}
~~
double bitwise not 双非位运算, 字符串转换成整数
let s = `3`;
// '3'
~s;
// -4
~~s;
// 3
+s;
// 3
shit test
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
const log = console.log;
function ListNode(val, next) {
this.val = 0 || val;
this.next = null || next;
}
log(`l1`, l1)
log(`l2`, l2)
const arr1 = [];
const arr2 = [];
while(l1) {
arr1.push(l1.val);
l1 = l1.next;
}
while(l2) {
arr2.push(l2.val)
l2 = l2.next;
}
const sum = parseInt(arr1.reverse().join(``)) + parseInt(arr2.reverse().join(``));
// 807
const arr = Array.from(sum + ``).reverse().map(i => parseInt(i));
// [7, 0, 8]
const LinkedList = (value) => {
const node = new ListNode(value, ``);
if(!head) {
head = node;
} else {
let current = head;
while(current.next) {
current = current.next;
}
current.next = node;
}
};
let head = null;
for (let i = 0; i < arr.length; i++) {
LinkedList(arr[i]);
}
log(`head`, head)
return head;
// return arr;
};
/*
好垃圾的测试呀,为什么参数给的不是 ListNode, 而是 array!
答案没有毛病呀
l1 [2,4,3]
l2 [5,6,4]
head ListNode {
val: 7,
next: ListNode { val: 0, next: ListNode { val: 8, next: '' } }
}
*/
链表生成器
// singly-linked list
class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
// add
// remove
}
// 链表生成
const linkedListGenerator = (arr) => {
let head;
for (const item of arr) {
const node = new ListNode(item);
if(!head) {
head = node;
} else {
let temp = head;
// 关键:迭代 next
while(temp.next) {
temp = temp.next;
}
temp.next = node;
}
}
return head;
}
大数相加
字符串相加,修复大数科学计数法的 bug ?
/*
[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].join('')
'1000000000000000000000000000001'
[5,6,4].join('')
'564'
[5,6,4].reverse().join('')
'465'
[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].reverse().join('')
'1000000000000000000000000000001'
parseInt('1000000000000000000000000000001');
1e+30
parseInt('1000000000000000000000000000001') + parseInt('465')
1e+30
sum = parseInt('1000000000000000000000000000001') + parseInt('465');
1e+30
`${sum}`.split('');
(5) ['1', 'e', '+', '3', '0']
科学计数法 ??? 字符串相加 ??? 进位
*/
https://leetcode.com/submissions/detail/661919117/
https://leetcode.com/problems/add-two-numbers/
https://leetcode.cn/problems/add-two-numbers/
refs
https://leetcode.com/problems/add-two-numbers/
https://leetcode-cn.com/problems/add-two-numbers/submissions/
https://leetcode-cn.com/submissions/detail/99618022/
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13528682.html
未经授权禁止转载,违者必究!