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 ...
随机推荐
- lvm快照
磁盘快照的使用(快照好像只可以使用一次,用过后自动删除) 首先在 /bplvm 下随便创建一个文件,如a.txt 然后执行命令 lvcreate -L 120M -s -n SNAP /dev/sto ...
- 第1章 Java语言概述--HelloWorld--环境搭建
SE学什么 第1章 Java语言概述 第2章 基本语法 第3章 数组 第4章 面向对象编程(上) 第5章 面向对象编程(中) 第6章 面向对象编程(下) 第7章 异常处理 第8章 枚举类&注解 ...
- [转]linux下编译boost.python
转自:http://blog.csdn.net/gong_xucheng/article/details/25045407 linux下编译boost.python 最近项目使用c++操作python ...
- 百度网盘提交提取密码:根据cookies获取loginId 的js
java下载百度网盘中资料,发现网上都只有关于公开分享的实现,没有关于私有分享的实现,抓包发现有一个关键参数:logid url:https://pan.baidu.com/share/verify? ...
- centos 7 su jenkins 切换不过去
root切换到jenkins用户: passwd jenkins:设置jenkins用户密码 su jenkins : 切换不过去, 查看passwd文件 cat /etc/passwd 找到:jen ...
- Azure VMSS (1) 入门
<Windows Azure Platform 系列文章目录> 在使用云计算服务的时候,我们经常需要有自动横向扩展的功能.比如: 1.在业务高峰期,根据负载的增加,自动打开若干台VM 2. ...
- 使有prometheus监控redis,mongodb,nginx,mysql,jmx
以下操作在CENTOS7环境. 使用prometheus做监控,使用grafana做dashboard的界面展示: 因prometheus自带的监控web界面图形化展示方面比较弱,推荐使用grafan ...
- 廖雪峰Java6 IO编程-2input和output-6classpath资源
1.从classpath读取文件可以避免不同环境下文件路径不一致的问题. Windows和Linux关于路径的表示不一致 Windows:C:\conf\default.properties Linu ...
- 在WINDOWS下安装MYSQL8.0
1:创建文件夹D:\data\service\mysql-8.0.11-winx64\data 2:进到D:\data\service\mysql-8.0.11-winx64\bin 第三步:初始化. ...
- 【比赛打分展示双屏管理系统-加强版】的两个ini配置文件功能解释及排行榜滚动界面的简答配置等
加强版目录下有两个ini文件,功能解释如下: 1. ScoreTip.ini: bScoreTip:如果为1,可以启用 回避 功能 或 高低分差值超出 iScoreRange 的 提示功能. iSco ...