[CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912.
LeetCode上的原题,请参见我之前的博客Add Two Numbers 两个数字相加。
跟那道LeetCode有所不同的是,这道题还有个Follow Up,把链表存的数字方向变了,原来是表头存最低位,现在是表头存最高位。既然是翻转了链表,那么一种直接的解法是把两个输入链表都各自翻转一下,然后用之前的方法相加完成,再把得到的结果翻转一次,就是结果了,翻转链表的方法可以参见我之前的博客Reverse Linked List 倒置链表。代码如下:
解法一:
// Follow up
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *dummy = new ListNode(-);
ListNode *cur = dummy;
int carry = ;
l1 = reverseList(l1);
l2 = reverseList(l2);
while (l1 || l2) {
int n1 = l1 ? l1->val : ;
int n2 = l2 ? l2->val : ;
int sum = n1 + n2 + carry;
carry = sum / ;
cur->next = new ListNode(sum % );
cur = cur->next;
if (l1) l1 = l1->next;
if (l2) l2 = l2->next;
}
if (carry) cur->next = new ListNode();
return reverseList(dummy->next);
}
ListNode *reverseList(ListNode *head) {
if (!head) return head;
ListNode *dummy = new ListNode(-);
dummy->next = head;
ListNode *cur = head;
while (cur->next) {
ListNode *tmp = cur->next;
cur->next = tmp->next;
tmp->next = dummy->next;
dummy->next = tmp;
}
return dummy->next;
}
};
如果我们不采用翻转链表的方法该怎么做呢,这就比较复杂了。首先我们要县分别计算出两个链表的长度,然后给稍短一点的链表前面补0,补到和另一个链表相同的长度。由于要从低位开始相加,而低位是链表的末尾,所以我们采用递归来处理,先遍历到链表的末尾,然后从后面相加,进位标示符carry用的是引用,这样保证了再递归回溯时值可以正确传递,每次计算的节点后面接上上一次回溯的节点,直到回到首节点完成递归。最后还是处理最高位的进位问题。代码如下:
解法二:
// Follow up
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int n1 = , n2 = , carry = ;;
n1 = getLength(l1);
n2 = getLength(l2);
if (n1 > n2) l2 = padList(l2, n1 - n2);
if (n2 > n1) l1 = padList(l1, n2 - n1);
ListNode *res = addTwoNumbersDFS(l1, l2, carry);
if (carry == ) {
ListNode *tmp = new ListNode();
tmp->next = res;
res = tmp;
}
return res;
}
ListNode *addTwoNumbersDFS(ListNode *l1, ListNode *l2, int &carry) {
if (!l1 && !l2) return NULL;
ListNode *list = addTwoNumbersDFS(l1->next, l2->next, carry);
int sum = l1->val + l2->val + carry;
ListNode *res = new ListNode(sum % );
res->next = list;
carry = sum / ;
return res;
}
ListNode *padList(ListNode *list, int len) {
ListNode *dummy = new ListNode(-);
ListNode *cur = dummy;
for (int i = ; i < len; ++i) {
cur->next = new ListNode();
cur = cur->next;
}
cur->next = list;
return dummy->next;
}
int getLength(ListNode *list) {
ListNode *cur = list;
int res = ;
while (cur) {
++res;
cur = cur->next;
}
return res;
}
};
[CareerCup] 2.5 Add Two Numbers 两个数字相加的更多相关文章
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- LeetCode 2. Add Two Numbers (两数相加)
题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
随机推荐
- less
//:此注释不会被编译 /**/:会被编译 变量:以@开头,exp:@test_width:100px; 混合: 匹配: 运算: 嵌套: arguments:将所有的变量带进去
- iOS之清除缓存
//清除缓存按钮的点击事件 - (void)putBufferBtnClicked:(UIButton *)btn{ CGFloat size = [self folderSizeAtPath:NSS ...
- 隐藏tabbar的属性hidesBottomBarWhenPushed
项目中有需求是A视图控制器push之后B视图控制器需要隐藏底部的tabbar,在pop之后A视图控制器仍然显示tabbar. 其实不需要在push操作时敲 self.hidesBottomBarWhe ...
- Java 内部类的阐述
创建一个Computer抽象类:用来在Test类中创建匿名抽象类 package com.zhiyou; public abstract class Computer { int a = 1; /** ...
- IOS 杂笔-16 (-(void)scrollViewDidEndScrollingAnimation:方法使用注意)
今天在写项目的时候,遇到了一件令人抓狂的事情. 正如标题所示,被这个方法弄的团团转. -(void)scrollViewDidEndScrollingAnimation:是协议里的方法. 意味当动画结 ...
- 基于Ruby的watir-webdriver自动化测试方案与实施(二)
接着基于Ruby的watir-webdriver自动化测试方案与实施(一) http://www.cnblogs.com/Javame/p/4159360.html 继续 ... ... 回顾 软 ...
- [Erlang 0104] 当Erlang遇到Solr
Joe Armstrong的访谈中有一段关于"打开黑盒子"的阐述,给我留下很深的印象:Joe Armstrong在做XWindows开发时没有使用对应的类库,而是在了解XW ...
- 干货发布:VSS文件清理工具
一.功能:1.删除VSS文件(以.vssscc,.scc,.vspscc为后缀的文件)2.去除文件解决方案文件和C#项目文件中的VSS标签3.删除Bin和Obj目录 二.开发工具:vs 2010 三. ...
- Windows 64位下装Oracle 11g,PLSQL Developer的配置问题,数据库处显示为空白的解决方案
安装pl sql 后,若下图的数据库处为空.则需要安装32位的客户端,说明pl sql不支持64位客户端连接. 解决办法: 1.下载32位Oracle客户端,并安装 2.设置PLSQL Develo ...
- postgres扩展开发
扩展开发的基本组成 demo--1.0.sql demo.c demo.control Makefile demo.c当中包含了自定义函数的实现,纯C语言,目录下可包含多个.c文件.demo-1.0. ...