LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List
题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list。
因为题目要求不能 reverse,可以把 两个list 的数字都 存入 两个stack。利用了stack的特性,就可以从后面把数字相加,具体看code。
Java Solution:
Runtime: 6 ms, faster than 51.06%
Memory Usage: 45.4 MB, less than 64.71%
完成日期:07/08/2019
关键点:stack
/**
* 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) { Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>(); // store two numbers into stacks
while(l1 != null) {
s1.push(l1.val);
l1 = l1.next;
} while(l2 != null) {
s2.push(l2.val);
l2 = l2.next;
} // adding two numbers from stacks
int sum = 0;
ListNode list = new ListNode(0); while(!s1.empty() || !s2.empty()) {
if(!s1.empty())
sum += s1.pop();
if(!s2.empty())
sum += s2.pop(); list.val = sum % 10; // get the value
ListNode head = new ListNode(sum / 10); // get the carry
head.next = list;
list = head; sum /= 10;
} // need to check if the head is 0 or not
return list.val == 0 ? list.next : list;
}
}
参考资料:LeetCode Discuss
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 445. Add Two Numbers II (两数相加 II)的更多相关文章
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 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 ...
- Java实现 LeetCode 445 两数相加 II
445. 两数相加 II 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会 ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445——两数相加 II
1. 题目 2. 解答 2.1 方法一 在 LeetCode 206--反转链表 和 LeetCode 2--两数相加 的基础上,先对两个链表进行反转,然后求出和后再进行反转即可. /** * Def ...
- Leetcode 445. 两数相加 II
1.题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. ...
随机推荐
- C#接口的作用实例解析
一.接口的作用: 我们定义一个接口: public interface IBark { void Bark(); } 1. 再定义一个类,继承于IBark,并且必需实现其中的Bark()方法 pub ...
- Dart编程实例 算术操作符
Dart编程实例 算术操作符 void main() { var num1 = 101; var num2 = 2; var res = 0; res = num1+num2; print(" ...
- Linux上VNC 启动和关闭
查询vnc的线程: [admin@cn2-uat-esb-01-0001 ~]$ ps -ef|grep vncadmin 19080 21305 0 10:04 pts/2 00:00:00 gre ...
- Lucene字段
字段是最低单元或索引过程的起点.它代表其中一个键被用于识别要被索引的值的键值对关系.用于表示一个文件的内容的字段中将具有键为“内容”和值,可以包含文本或文档的数字内容的部分或全部. Lucene可以索 ...
- GP下CalculateField的用法
以前用过这个类做字段计算,许久不用有些忘却,记录一下使用方式 public static void CalculateField(IFeatureLayer featureLayer,IField f ...
- ArcGis相接面补节点c#
相接(Touch)面执行切割后 新面与原相接面会缺少公共节点. private void AddPointToTouchesPolygon(IFeatureCursor newFeatureCurso ...
- plsql创建一个表、序列、和触发器
plsql创建表后不能直接让id递增,因此要手动创建,下面是例子: 1.创建表 SQL: create table student(id number primary key,name varchar ...
- Vue双向数据绑定原理深度解析
首先,什么是双向数据绑定?Vue是三大MVVM框架之一,数据绑定简单来说,就是当数据发生变化时,相应的视图会进行更新,当视图更新时,数据也会跟着变化. 在分析其原理和代码的时候,大家首先了解如下几个j ...
- java oop第11章_反射、BaseDao的进一步改造
引言:从Java5开始,Java中引用了一个新的概念反射,当程序运行时,能动态感知到程序中拥有的所以信息,这个获取信息的过程是采用反射机制来完成. 一. Class类: Class类用 ...
- 前端 css 进阶
内容目录: 1.css盒子模型 2.浮动 float 3.溢出 overflow 4.定位 position 5. z-index 6.opacity 1.css盒子模型 margin:用来调节盒子与 ...