LeetCode-2: Add Two Numbers
【Problem:2-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.
【Example】
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
【Solution】
1)From 九章算法:(can run, but "Submission Result: Wrong Answer!")
class Solution:
def addTwoNumbers(self, l1, l2):
head = ListNode(0)
ptr = head
carry = 0
while True:
if l1 != None:
carry += l1.val
l1 = l1.next
if l2 != None:
carry += l2.val
l2 = l2.next
ptr.val = carry % 10
carry /= 10
# 运算未结束新建一个节点用于储存答案,否则退出循环
if l1 != None or l2 != None or carry != 0:
ptr.next = ListNode(0)
ptr = ptr.next
else:
break
return head
2)Java :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null) {
return null;
} ListNode head = new ListNode(0);
ListNode point = head;
int carry = 0;
while(l1 != null && l2!=null){
int sum = carry + l1.val + l2.val;
point.next = new ListNode(sum % 10);
carry = sum / 10;
l1 = l1.next;
l2 = l2.next;
point = point.next;
} while(l1 != null) {
int sum = carry + l1.val;
point.next = new ListNode(sum % 10);
carry = sum /10;
l1 = l1.next;
point = point.next;
} while(l2 != null) {
int sum = carry + l2.val;
point.next = new ListNode(sum % 10);
carry = sum /10;
l2 = l2.next;
point = point.next;
} if (carry != 0) {
point.next = new ListNode(carry);
}
return head.next;
}
} // version: 高频题班
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
// write your code here
ListNode dummy = new ListNode(0);
ListNode tail = dummy; int carry = 0;
for (ListNode i = l1, j = l2; i != null || j != null; ) {
int sum = carry;
sum += (i != null) ? i.val : 0;
sum += (j != null) ? j.val : 0; tail.next = new ListNode(sum % 10);
tail = tail.next; carry = sum / 10;
i = (i == null) ? i : i.next;
j = (j == null) ? j : j.next;
} if (carry != 0) {
tail.next = new ListNode(carry);
}
return dummy.next;
}
}
3)C++:yes, accepted
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// 题意可以认为是实现高精度加法
ListNode *head = new ListNode();
ListNode *ptr = head;
int carry = ;
while (true) {
if (l1 != NULL) {
carry += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
carry += l2->val;
l2 = l2->next;
}
ptr->val = carry % ;
carry /= ;
// 当两个表非空或者仍有进位时需要继续运算,否则退出循环
if (l1 != NULL || l2 != NULL || carry != ) {
ptr = (ptr->next = new ListNode());
} else break;
}
return head;
}
};
【References】
1、http://www.jiuzhang.com/solution/add-two-numbers/
Time complexity:O(n^2)2
LeetCode-2: Add Two Numbers的更多相关文章
- LeetCode OJ:Add Two Numbers (相加链表之数)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- Q2:Add Two Numbers
2. Add Two Numbers 官方的链接:2. Add Two Numbers Description : You are given two non-empty linked lists r ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- leetcode:Add Two Numbers
题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...
随机推荐
- 使用pyplot和seaborn进行画图
pyplot的一些知识 matplotlab中的对象: matplotlib是面向对象的,在画图的时候我们了解一些对象,对我们画图是有帮助的.绘图的对象大致分为三层: backend_bases.Fi ...
- ReactNative 调用手机地图(高德、百度)导航 Android
由于项目需要,鉴于第三方sdk包的体积略大,经过评估论证后,决定采用调起APP以及网页地图的方式来进行导航. 思路: 在需要调用导航的界面通过原生获取当前手机内可用的导航app组装成列表返回到RN待选 ...
- 开发Google Material Design风格的WPF程序
今天在网上看到了一个Material Design风格的WPF皮肤,看上去还是挺不错的 这个项目是开源的,感兴趣的朋友可以下载试下: https://github.com/ButchersBoy/Ma ...
- IDA Bitfields
Bitfields There is a special kind of enums: bitfields. A bitfield is an enum where the 32bits are di ...
- linux防火墙查看、开启、关闭
查看 vi /etc/sysconfig/iptables 开启 service iptables stop 关闭 service iptables restart
- OpenGl的源程序,运行就提示,"计算机丢失 glut32.dll文件"
转自:http://www.cppblog.com/longzxr/archive/2009/12/04/102565.html?opt=admin 今天调试OpenGl的源程序,编译通过,但一运行就 ...
- sqlserver锁机制详解(sqlserver查看锁)
简介 在SQL Server中,每一个查询都会找到最短路径实现自己的目标.如果数据库只接受一个连接一次只执行一个查询.那么查询当然是要多快好省的完成工作.但对于 大多数数据库来说是需要同时处理多个查询 ...
- JS中实现字符串和数组的相互转化
早上起来看了一道js的面试题,是这样描述的:利用var s1=prompt("请输入任意的字符串","")可以获取用户输入 的字符串,试编程将用户输入的字符串“ ...
- 惠普HP compaq康柏系列 CQ40笔记本电脑拆机除尘
工具:两用螺丝刀(一字口的拆CPU,十字口的拆其它所有螺丝) 散热硅胶和CPU上的散热贴 正面照(A面) 反面照(D面) 第一步:拆掉电池,不要忘记了红圈这里的两颗螺丝.共6颗小螺丝. 第二步: ...
- Js组件layer的使用
作为独立组件使用 layer 引入好layer.js后,直接用即可 <script src="layer.js"></script> <script& ...