LeetCode & linked list bug
LeetCode & linked list bug
add-two-numbers
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: '' } }
}
*/
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 发布文章使用:只允许注册用户才可以访问!
LeetCode & linked list bug的更多相关文章
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode 全解(bug free 训练)
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- LeetCode——Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- LeetCode——Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
随机推荐
- jQuery 真伪数组的转换
//真数组转换伪数组 var arr = [1,3,5,7,9]; var obj = {}; [].push.apply(obj,arr); console.log(obj) //伪数组转真数组 v ...
- Lua大量字符串拼接方式效率对比及原因分析
Lua大量字符串拼接方式效率对比及原因分析_AaronChan的博客-CSDN博客_lua字符串拼接消耗 https://blog.csdn.net/qq_26958473/article/detai ...
- LINUX动态库(.SO)搜索路径(目录)设置方法
LINUX动态库(.SO)搜索路径(目录)设置方法 [root@VM_0_11_centos ld.so.conf.d]# cat /etc/ld.so.confinclude ld.so.conf. ...
- CPU处理器架构和工作原理浅析
CPU处理器架构和工作原理浅析 http://c.biancheng.net/view/3456.html 汇编语言是学习计算机如何工作的很好的工具,它需要我们具备计算机硬件的工作知识. 基本微机设计 ...
- Redis 实战 —— 10. 实现内容搜索、定向广告和职位搜索
使用 Redis 进行搜索 P153 通过改变程序搜索数据的方式,并使用 Redis 来减少绝大部分基于单词或者关键字进行的内容搜索操作的执行时间. P154 基本搜索原理 P154 倒排索引 (in ...
- Prometheus 监控之 Blackbox_exporter黑盒监测
Prometheus 监控之 Blackbox_exporter黑盒监测 1.blackbox_exporter概述 1.1 Blackbox_exporter 应用场景 2.blackbox_exp ...
- Docker 之 Jenkins自动化部署
Docker 之 Jenkins自动化部署 Jenkins部署 jenkis 绑定gitlab shell脚本自动化构建Docker镜像 提升maven构建速度 jenkins 无法通过shell脚本 ...
- Scala中使用fastJson 解析json字符串
Scala中使用fastJson 解析json字符串 添加依赖 2.解析json字符 2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象 2.2然 ...
- jackson学习之七:常用Field注解
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Hyperbase常用SQL
1.创建表 1.1 建HBase内表 CREATE TABLE hbase_inner_table( key1 string, bi bigint, dc decimal(10,2), ...