LeetCode 第二题 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.
给你两个非空链表,每个节点存储一位值,是反过来存储的。用同样的存储方式返回他们的和。
思路
直接模拟,高精度加,考察链表。裸题。
时间复杂度 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 大整数加法 高精度加法 链表的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- 乘风破浪:LeetCode真题_002_Add Two Numbers
乘风破浪:LeetCode真题_002_Add Two Numbers 一.前言 这次的题目是关于链表方面的题目,把两个链表对应节点相加,还要保证进位,每个节点都必须是十进制的0~9.因此主要 ...
- c++ 超长整数加法 高精度加法
c++ 超长整数加法 高精度加法 实现思路 不能直接使用加法,因为int和long long都已超出最大数据表示范围 数据读入采用string类型,读入后将数据的每一位存储到vector中 vecto ...
- LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)
这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- LeetCode算法题-Self Dividing Numbers(Java实现)
这是悦乐书的第305次更新,第324篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第173题(顺位题号是728).自分割数是一个可被其包含的每个数字整除的数字.例如,12 ...
随机推荐
- 2014.1.21 DNS大事故(dns原理、网络封锁原理)
1.21那天发生了什么,由1.21联想补充…… 很多网站都上不去,域名解析都到了65.49.2.178这个IP地址 先科普,再深挖 dns查询类型 递归查询,迭代查询 DNS解析过程,这里使用 ...
- linux的目录结构知识
1. Linux的目录结构知识图解 2. linux下应用程序组成及文件目录定义 #二进制程序: /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/l ...
- 解决 webpack-dev-server 不能使用 IP 访问
webpack 是众所周知很好用的打包工具,在开发 vue 项目时,vue-cli 就集成了 webpack.我们启一个服务:npm run dev然后在浏览器可是使用 http://localhos ...
- MySql基础补漏笔记
在MySQL教程|菜鸟教程系统复习的时候有一些知识点还没掌握透的或者思维方式还没完全跟上的地方,写了一个笔记,讲道理此笔记对除我之外的任何读者不具有任何实用价值,只针对我在复习MySQL基础过程中的查 ...
- 实训第八天 有关python orm 的学习记录 常用方法02
继续沿用第七天数据库:def test2(request): # 1.xxx__lt 小于 :查询出年龄小于22的所有 ret=models.Person.objects.filter(age__lt ...
- 图解css3学习笔记
(0)css3是啥 css3是最新版本的css,添加了许多新的特性,将切图仔从繁重的工作中解救出来. css3现在主流的浏览器大部分都支持(ie9部分支持,ie8之前的都不支持) 渐进增强,优雅降级 ...
- JavaScript数据类型typeof()和转换
javascript属于弱类型,值包含:数字,字符串和布尔值,c++与java属于强类型;int a="a",string a="a" 报错;var a ;原始 ...
- 「Flink」使用Managed Keyed State实现计数窗口功能
先上代码: public class WordCountKeyedState { public static void main(String[] args) throws Exception { S ...
- Windows Server 2012 R2的安装(GUI桌面版本)
镜像:cn_windows_server_2012_r2_x64_dvd_2707961.iso 1.将安装光盘插入服务器,开机会读取到Windows安装程序,点击下一步 2.点击现在安装 3 ...
- Java连载88-HashSet集合与hashCode方法重写
一.Set集合 1.HashSet底层实际上是一个HashMap,HashMap底层采用了哈希表数据结构. 2.哈希表又称为散列表,哈希表底层是一个数组,这个数组中每一个元素是一个单向链表,每个单向链 ...