445. Add Two Numbers II 链表中的数字求和
[抄题]:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
最后看第一位是不是0,不是0才能返回。
[思维问题]:
顺序是反的,不知道用stack
通过sum / 10的方法可以把十位取出来,不用清空,每次除10一直加就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
顺序是反的就要用stack求和
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 先初始化一个待添加列表list,然后余数都往后面加。求和的头都放在head中。
[二刷]:
- list.val = sum % 10; 可以直接往用等号list后面加点,不知道怎么来的
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
通过sum / 10的方法可以把十位取出来,不用清空,每次除10一直加就行了。倒序相加用stack
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//initialization: 2 stacks
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>(); //corner case
if (l1 == null && l2 == null) return null; //put into stacks
while (l1 != null) {
stack1.add(l1.val);
l1 = l1.next;
} while (l2 != null) {
stack2.add(l2.val);
l2 = l2.next;
} //while loop
//get sum, val, head, append the previous val to head, give head to val, reset sum
ListNode list = new ListNode(0);
int sum = 0;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
//get sum
if (!stack1.isEmpty()) sum += stack1.pop();
if (!stack2.isEmpty()) sum += stack2.pop(); //get the val node
list.val = sum % 10; ListNode head = new ListNode(sum / 10);
//append the previous val to head, head is ready
head.next = list;
//give head to val
list = head; sum /= 10;
} //return
return list.val == 0 ? list.next : list;
}
}
445. Add Two Numbers II 链表中的数字求和的更多相关文章
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 445. Add Two Numbers II【Medium】【两个链表求和】
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445. Add Two Numbers II(链表求和)
题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...
- *445. Add Two Numbers II
1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- Spark Streaming 'numRecords must not be negative'问题解决
转载自:http://blog.csdn.net/xueba207/article/details/51135423 问题描述 笔者使用spark streaming读取Kakfa中的数据,做进一步处 ...
- docker centos7创建consul镜像以及用docker-compose启动镜像
直接贴代码了: Dockfile: # Version 0.1 FROM kuba_centos7 MAINTAINER kuba si812cn@163.com # This is the rele ...
- 依赖、耦合、解耦、控制反转(IOC)、依赖注入(DI)
随着net的深入学习,出现了很多概念性的东西需要理解,现在统一记录一下. 1.依赖:现阶段在任何一个有请求作用的系统,都会出现A类调用B类的情况,这时候A类就依赖于B类,A类和B类存在依赖关系. 2. ...
- 5.IAP - FLASH
一.Flash与时钟系统的关系 STM32系统时钟: HSE 高速外部时钟,电路上焊接的外部时钟,一般是4Mhz-16Mhz,板子上的是8Mhz ...
- Excel函数匹配查找
需求 例如:北京沃尔玛有限公司,由已知的沃尔玛缩写,将两者进行匹配. 函数 lookup() 第一个参数“Lookup_value”:是要查找的值:第二个参数“lookup_vector”:是要查找的 ...
- [转][C#]压缩解压
{ internal static class Compressor { public static Stream Decompress(Stream source, bool bidiStream) ...
- hash 在 perl 中的用法(转载)
Perl的数据结构中最有趣的一个特性是哈希(hash),它使得在数据片段之间建立键-值(key-value)关联成为可能.虽然这些哈希要远远比普通系统中以数字索引的数组用途更广,但是往往也会使初学者不 ...
- pt-table-checksum校验与pt-table-sync修复数据
1:下载工具包 登录网站下载相应的工具包 https://www.percona.com/downloads/percona-toolkit/LATEST/ 2:安装 (1)yum安装: sudo y ...
- OpenStack Mitaka/Newton/Ocata/Pike 各版本功能贴整理
逝者如斯,刚接触OpenStack的时候还只是第9版本IceHouse.前几天也看到了刘大咖更新了博客,翻译了Mirantis博客文章<OpenStack Pike 版本中的 53 个新功能盘点 ...
- DELL服务器r710配置RAID
DELL服务器r710配置RAID: 1.开机后等待DELL图标出现,连续按ctrl+r,进入RAID配置界面: 2.进入RAID配置界面,如果是下面的这个界面,选择Disk group 0这项需要按 ...