2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

给两个非负整数以倒叙存储的非空链表,每个节点一个数字,两个数字不以0开头(出本身为0)

这题给我了较大的困扰,因为之前确实没接触过js的链表(准确的说都不知道js有链表,惭愧...)

一开始我天真的以为,应该就是数组模拟的吧(没注意),嗯,这题简单,数组翻转-拼接字符串-转数字-相加-转字符串-转数组-翻转-返回。然后一跑,直接懵逼。说reverse不是个方法。

又不在本地,调试不了,只能一开始就 return 参数,打出来是个数组,return Object.prototype.toString(l1)打出来的是个对象。

无奈只能先研究链表,发现实现其实到不算复杂,一个节点一个对象;两个属性,一个存值一个存下一个节点的地址,最后一个节点next为null。

然后我就开始尝试解题了,然后又懵逼

//解法依旧原始,把链表转数组,然后开始一开始的步奏
//结果证明太天真
/**
* 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) {
var arr1=[],arr2=[],i,l=[];
arr1[arr1.length] = l1.val;
while(l1.next){
l1 = l1.next;
arr1[arr1.length] = l1.val;
}
arr2[arr2.length] = l2.val;
while(l2.next){
l2 = l2.next;
arr2[arr2.length] = l2.val;
}
var sum = (parseInt(arr1.reverse().join(''),10)+parseInt(arr2.reverse().join(''),10) +'').split('');
for(i in sum){
l[i] = parseInt(sum[sum.length -1 -i]);
}
return l
};

error code

本地跑没毛病,提交就出现了这个

第一反应是这是什么鬼。仔细看明白是科学记数法。这又是个难题,哪怕把科学计数法转回数字,可怎么解决精度不够被省略的部分呢。

//看着那一大长串数字,突然灵机一现,这个东西应该不需要转成数字计算
//思考:加法运算从个位开始,相同位数相加,满十进一,那这刚好倒叙的链表不是可以让我们从个位开始依次访问吗
//思路有了,解题过程中,却由于粗心和对链表实现的不熟悉,花了几十分钟才算是解决
/**
* 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) {
var next = {},//一下个节点
list = next,//目标链表,作为链表入口,值不能修改
sum = 0,//良数同一位数组之合+进位的数
carry = 0,//进位的数,1为满十进一
arr = [];//最后返回的数组,这题很奇怪,输入的是链表,返回却需要是数组 function add(cur, val, noNext) {//添加链表节点的函数
cur.val = val;
cur.next = noNext ? null : {};
cur = cur.next;//利用引用类型存地址不存实际值,使变量next始终指向下一个节点
return cur;
}
sum = l1.val + l2.val;
while ((l1 && l1.next) || (l2 && l2.next) || carry) {
//把个位计算放在循环外面,把借点添加放在循环里面的意义在于:如果添加节点放在循环前面,我们并不能知道还有没有下一个节点,而放在循环里面,只要能进循环,说明还有下一个节点,可以放心添加,然后在节点后面添加最后的节点。
//简单说,就是先计算值,等知道是否有下一个节点后再添加节点
if (sum >= 10) {
next = add(next, sum % 10);
carry = 1;
} else {
next = add(next, sum);
} l1 = l1 && l1.next;
l2 = l2 && l2.next;
sum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
carry = 0;
}
if (sum >= 10) {
next = add(next, sum % 10);
next = add(next, 1, true);//如果输入值的最后数字相加进一,所进的一才是最后的节点
} else {
next = add(next, sum, true);
}
arr[arr.length] = list.val;
while (list.next) {//链表转数组
list = list.next;
arr[arr.length] = list.val;
}
return arr;
};

然后研究了热门解法,发现计算方式和人家一样,但是人家的比我优美多了,其实不需要判断合是否大于十,直接用10做取余、除(js需要一点处理)也就不需要变量来记录进1。不过由于实现方式大致相同,时间关系我也就没有用js写一遍了,热门解法在下面,可以欣赏下

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode sentinel = new ListNode(0);
ListNode d = sentinel;
int sum = 0;
while (c1 != null || c2 != null) {
sum /= 10;
if (c1 != null) {
sum += c1.val;
c1 = c1.next;
}
if (c2 != null) {
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum % 10);
d = d.next;
}
if (sum / 10 == 1)
d.next = new ListNode(1);
return sentinel.next;
}
}

LeetCode解题笔记 - 2. Add Two Numbers的更多相关文章

  1. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

  2. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  3. 【leetcode刷题笔记】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  5. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  6. leetcode第四题--Add Two Numbers

    Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...

  7. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

  8. LeetCode第二题:Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. LeetCode解题笔记 - 1. Two Sum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. IP地址的格式和分类

    IP地址 IP地址时IP协议提供的一种地址格式,它为互联网上的网络设备分配一个用来通信的逻辑地址,目前分为IP v4和IP v6两种,v4的意思是version4,v6是同样的意思. IP v4 IP ...

  2. Mybatis的小技巧

    一.使用resultMap字段关联对象属性太麻烦 eg:过于复杂,类似这种结果集转换的,只需要在配置文件中开启自动转换进行了,无需再手动写了很麻烦 替换办法:开启骆驼命名法进行匹配就ok了,实体类字段 ...

  3. js中关于带数字类型参数传参丢失首位数字0问题

    最近在项目中遇到一个问题,js中传带有数字的参数时,如果参数开头有数字0,会把0给去掉. 例如: 方法abc(0123456,789); 方法abc中获取的参数0123456就会变为123456. 原 ...

  4. screen工具的安装与使用

    yum install screen    安装screen screen -S <作业名称>     创建新的页 screen -ls   查询已经存在的页面 screen -r < ...

  5. MySQL数据库:函数的应用

    字符串截取 # 从左边开始 第1个字符 left(字段名,1) # 从那里开始,截取几个 substring(字段名,1,1) str函数 # 连接字符串 concat(s1,s2,s3,--,sn) ...

  6. 表单生成器(Form Builder)之mongodb表单数据查询——统计查询求和

    上一篇笔记仅是记录了一下简单的关联查询,根据笔记中的场景:将某一车辆关联的耗损记录全部放在了一个字段当中.不知道现在中有没有这种场景,我们的应用中没有类似的场景,可能我们更关注的是某车辆的总耗损金额和 ...

  7. shell 脚本里的$(( ))、$( )、``与${ }的区别

    shell  脚本里的命令执行 1. 在bash中,$( )与` `(反引号)都是用来作命令替换的. 命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组 ...

  8. CCPC 2019 秦皇岛 Angle Beats

    题目 给出P个点,然后给出Q个询问,问从P中选出两个点和给的点能组成直角三角形的方法个数.-O2,时间限制5秒. \[2\leqslant P\leqslant 2000,\qquad 1\leqsl ...

  9. 目前下载VS2017你可能会遇到这个坑

    可能现在大伙都已经开始使用VS2019进行开发了.VS2019的下载使用也都很简单.由于工作需要,今天要在笔记本上安装VS2017,结果发现,VS2017的下载变得不是那么容易了,官方的下载方式也隐藏 ...

  10. 解决root无法登陆

    今天重装了一下虚拟机,用filezilla往Linux扔文件需要用root的超级权限,但是却不能建立连接,使用账号密码也无法登录root账户 鼓捣好一阵才知道,因为root权限太高了,可以针对root ...