题意

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.

给你两个非空链表,每个节点存储一位值,是反过来存储的。用同样的存储方式返回他们的和。

思路

直接模拟,高精度加,考察链表。裸题。

时间复杂度 O(max(L1,L2))O(max(L1,L2))O(max(L1,L2))

源码

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode r = null;
int t = 0;
int s;
while (l1 != null && l2 != null) {
s = l1.val + l2.val + t;
l1 = l1.next;
l2 = l2.next;
t = s/10; s %= 10;
if (r == null) {
r = new ListNode(s);
head = r;
} else {
r.next = new ListNode(s);
r = r.next;
}
}
ListNode rest;
if (l1 != null)
rest = l1;
else if (l2 != null)
rest = l2;
else {
// case 3
if (t > 0)
r.next = new ListNode(t);
return head;
}
while (rest != null) {
s = rest.val + t;
rest = rest.next;
t = s/10; s %= 10;
r.next = new ListNode(s);
r = r.next;
}
if (t > 0)
r.next = new ListNode(t);
return head;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode r = null;
int t = 0;
int s;
while (l1 != null || l2 != null || t > 0) {
s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + t;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
t = s/10; s %= 10;
if (r == null) {
r = new ListNode(s);
head = r;
} else {
r.next = new ListNode(s);
r = r.next;
}
}
return head;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head,r;
head = r = new ListNode(0);
int t = 0;
int s;
while (l1 != null || l2 != null || t > 0) {
s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + t;
l1 = (l1 == null ? null : l1.next);
l2 = (l2 == null ? null : l2.next);
t = s/10;
r = r.next = new ListNode(s%10);
}
return head.next;
}
}

时间比较及分析

结果比较

3次的结果分别是

Runtime: 31 ms, faster than 38.95% of Java online submissions for Add Two Numbers.

Memory Usage: 43.3 MB, less than 100.00% of Java online submissions for Add Two Numbers.

Runtime: 23 ms, faster than 67.88% of Java online submissions for Add Two Numbers.

Memory Usage: 48.1 MB, less than 100.00% of Java online submissions for Add Two Numbers.

Runtime: 21 ms, faster than 92.00% of Java online submissions for Add Two Numbers.

Memory Usage: 47.9 MB, less than 100.00% of Java online submissions for Add Two Numbers.

分析

第一次提交只有38.95%不到60%,由于时间复杂度已经是线性了,而且理论上复杂度不能再低了,因此推测应该是——

我人丑代码丑,自带大常数。



于是我想了一下,改成了版本2.过60%了。

我觉得就可以了。

^_^

LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表的更多相关文章

  1. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  2. LeetCode第二题—— Add Two Numbers(模拟两数相加)

    Description: You are given two non-empty linked lists representing two non-negative integers. The di ...

  3. Leetcode 第 2 题(Add Two Numbers)

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

  4. 乘风破浪:LeetCode真题_002_Add Two Numbers

    乘风破浪:LeetCode真题_002_Add Two Numbers 一.前言     这次的题目是关于链表方面的题目,把两个链表对应节点相加,还要保证进位,每个节点都必须是十进制的0~9.因此主要 ...

  5. c++ 超长整数加法 高精度加法

    c++ 超长整数加法 高精度加法 实现思路 不能直接使用加法,因为int和long long都已超出最大数据表示范围 数据读入采用string类型,读入后将数据的每一位存储到vector中 vecto ...

  6. LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)

    这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...

  7. LeetCode算法题-Add Binary(Java实现)

    这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...

  8. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  9. LeetCode算法题-Self Dividing Numbers(Java实现)

    这是悦乐书的第305次更新,第324篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第173题(顺位题号是728).自分割数是一个可被其包含的每个数字整除的数字.例如,12 ...

随机推荐

  1. 杭电--1009 C语言实现

    思路:要用有限的猫粮得到最多的javabean,则在房间中得到的javabean比例应尽可能的大. 用一个结构体,保存每个房间中的javabean和猫粮比例和房间号,然后将结构体按比例排序,则从比例最 ...

  2. 秘钥分割-Shamir秘钥分割门限方案

    精选: 1.问题的提出 2.需求的抽象: 有一个秘钥S,转换成另一种数据数据形式,分配给12个人(s1,s2,.......,s12),使得任意3个人的数据拼凑在一起就可以反向计算出秘钥S. 3.解决 ...

  3. 使用 Apache James 3.3.0(开源免费) 搭建外网电子邮件服务器(基于 Windows + Amazon Corretto 8)

    对于邮件服务器的安装,请先参阅: 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8) https://www ...

  4. Spark基础和RDD

    spark 1. Spark的四大特性 速度快 spark比mapreduce快的两个原因 基于内存 1. mapreduce任务后期在计算的是时候,每一个job的输出结果都会落地到磁盘,后续有其他的 ...

  5. 6.python设置代理和添加镜像源介绍

    为什么要修改镜像源? 一般使用python安装库,会用到pip install xxx 指令或者conda install xxx指令,因为pip和conda默认国外镜像源,这时会在Python的官方 ...

  6. js对象模型2

    g

  7. Umi 小白纪实(三)—— 震惊!路由竟然如此强大!

    在<Umi 小白纪实(一)>中有提到过简单的路由配置和使用,但这只是冰山一角 借用一句广告词,Umi 路由的能量,超乎你的想象 一.基本用法 Umi 的路由根结点是全局 layout  s ...

  8. vim编辑超大文件

    进入大文件(12g,250w+ lines),vim,耐心等待 有两种方法编辑删除冗余字段 1.set number ,可以通过:+数字组合跳到指定行,输入命令   ":100,200d&q ...

  9. 【macOS使用技巧】使用空格键快速预览文件内容

    Quickview 是mac系统上一个强大的预览功能, 可以预览 mp4 mov等音频文件, 当然图片.文本.也都可以进行预览. 在系统中如果你希望快速浏览一下文件而不想打开的文件的话只要选择文件然后 ...

  10. youtube使用youtube-dl下载挂代理的方法

    youtube-dl.exe --no-check-certificate --proxy=127.0.0.1:1080 --external-downloader=aria2c --external ...