[leetCode][016] Add Two Numbers
【题目】:
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
【题意】:
首先题意理解,输入为两个单链表,每一个链表的元素均为0~9的数字,而每一个链表示一个数字,头部是最低位,尾部是最高位。例如上述题目中Input的两个链表分别表示342 以及 465.
那么问题就来了,输出为新的链表,该链表以同样的方式表示输入的两个数字之和。
【Key Point】:
记录进位
【坑】:
可以写两个方法,将链表内容转换成数字,以及将数字转换成对应的链表。于是就有了以下步骤:
- list1, list2 <convertListToNum> num1, num2;
- value = num1 + num2;
- value <convertNumToList> newList;
如果题目中规定了链表的长度,这种方法未尝不可,可是没有~ 计算机中无论int(最大表示到2^4),long(最大也表示到2^4),long long(最大表示到2^8)等都有极限,而链表理论上可以形成很大的数字,很容易就能够突破 , 所以使用数值类型来存储结果是不可能通过所有case的。因此可以使用非数值型来存储,但是无形中增加了该题目的复杂度。
【解答】:
既然题目给的链表结果就是从低位向高位(很舒服,如果逆向又得费点事),逐次针对链表两个元素进行相加,记录每一次的进位,注意 题目并没有说输入的两个链表长度一致。以下是代码实现,通过leetCode的OJ
class Solution {
public:
// 记录每一次的进位,这样就突破了数值型的限制
ListNode *addTwoNumbers2(ListNode *l1, ListNode *l2){
ListNode *root = NULL, *pre = NULL;
int nCarry = ; // 每一个数位能够产生的进位最多为1,所以可以采用bool或者1bit表示
while (NULL != l1 || NULL != l2){
int nodeVal = ;
if (NULL == l1){
nodeVal = l2->val + nCarry;
l2 = l2->next;
}
else if (NULL == l2){
nodeVal = l1->val + nCarry;
l1 = l1->next;
}
else{
nodeVal = l1->val + l2->val + nCarry;
l1 = l1->next;
l2 = l2->next;
}
if (nodeVal >= ){ // 产生进位
nCarry = ;
nodeVal %= ;
}
else{
nCarry = ; // 进位清零
}
ListNode *node = new ListNode(nodeVal);
if (pre == NULL){
root = node;
}
else{
pre->next = node;
}
pre = node; // 记录上次节点,串联整个链表使用
}// while
if (nCarry != ){ // 当链表结束如果还有进位,需要增加一个最高位节点
ListNode * lastNode = new ListNode(nCarry);
if (NULL != pre){
pre->next = lastNode;
}
}// nCarry != 0
return root;
}
}
【leetCode Submission】

【运行结果】:

如果有什么问题,希望各位不吝赐教,小弟感恩答谢!
[leetCode][016] Add Two Numbers的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
随机推荐
- Mysql_以案例为基准之查询
查询数据操作
- linux ls正则表达式
ls就是默认排序的. 所以: ls只支持通配符,不支持正则,所以单纯用ls是不能实现的. 一些正则过滤操作需要结合支持正则的命令如grep.sed或awk. 例如:ls | grep "[0 ...
- Two Sum I & II
Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...
- 用几条shell命令快速去重10G数据
试想一下,如果有10G数据,或者更多:怎么才能够快速地去重呢?你会说将数据导入到数据库(mysql等)进行去重,或者用java写个程序进行去重,或者用Hadoop进行处理.如果是大量的数据要写入数据库 ...
- jquery validate自定义checkbox验证规则和样式
参考:http://blog.csdn.net/xh16319/article/details/9987847 自定义checkbox验证,“检查checkbox是否选中” jQuery.valida ...
- coco2dx实现翻拍效果
昨天吃饭看见同事演示他做的翻牌效果,感觉不错,我心血来潮也来搞个,很简单直接上代码. class FlipCard : public CCNode { public: CREATE_FUNC(Flip ...
- iOS NSOperation的使用
先给出NSOpetation的官方指导https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation ...
- iOS 基于UIWebView的应用特点
现在有许多ios应用都是基于UIWebView的应用,比如phonegap做出的那些程序.最近我也接触到了一个类似的程序,今天在这里分享一下这类程序的特点. 用UIWebView来实现程序,自然少不了 ...
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【动态规划】盖房子(house)--未提交--已提交
问题 D: 盖房子(house) 时间限制: 1 Sec 内存限制: 64 MB提交: 27 解决: 16[提交][状态][讨论版] 题目描述 FJ最近得到了面积为n*m的一大块土地,他想在这块土 ...