[Leetcode Week15] Add Two Numbers
Add Two Numbers 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/add-two-numbers/description/
Description
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
Explanation: 342 + 465 = 807.
Solution
class Solution {
private:
ListNode* copyList(ListNode* listHeadNode) {
if (listHeadNode == NULL)
return NULL;
ListNode* resHeadNode = new ListNode(listHeadNode -> val);
ListNode* resCurNode = resHeadNode;
ListNode* listCurNode = listHeadNode -> next;
while (listCurNode != NULL) {
resCurNode -> next = new ListNode(listCurNode -> val);
resCurNode = resCurNode -> next;
listCurNode = listCurNode -> next;
}
return resHeadNode;
}
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return copyList(l2);
if (l2 == NULL)
return copyList(l1);
bool isOverflow = false;
int curVal = l1 -> val + l2 -> val;
if (curVal >= 10) {
isOverflow = true;
}
ListNode* resHeadNode = new ListNode(curVal % 10);
ListNode* resPreNode = resHeadNode;
ListNode* l1CurNode = l1 -> next;
ListNode* l2CurNode = l2 -> next;
while (l1CurNode != NULL && l2CurNode != NULL) {
curVal = l1CurNode -> val + l2CurNode -> val;
if (isOverflow)
curVal++;
resPreNode -> next = new ListNode(curVal % 10);
isOverflow = (curVal / 10 > 0);
resPreNode = resPreNode -> next;
l1CurNode = l1CurNode -> next;
l2CurNode = l2CurNode -> next;
}
if (l1CurNode == NULL)
resPreNode -> next = copyList(l2CurNode);
else
resPreNode -> next = copyList(l1CurNode);
if (isOverflow) {
if (resPreNode -> next == NULL) {
resPreNode -> next = new ListNode(1);
} else {
while (true) {
if (resPreNode -> next == NULL) {
resPreNode -> next = new ListNode(1);
break;
} else {
curVal = resPreNode -> next -> val + 1;
if (curVal >= 10) {
resPreNode -> next -> val = curVal % 10;
resPreNode = resPreNode -> next;
} else {
resPreNode -> next -> val = curVal;
break;
}
}
}
}
}
return resHeadNode;
}
};
解题描述
这道题可以说解决的是数字相加的问题,但是由于数字的长度不确定,输入的数字可能是现有的内置数据类型无法存储的,所以像题目中给出的方法是,利用链表来记录数字,然后针对链表中的每一个节点的值进行相加。
明确了题目的要求之后,很快就可以清楚,题目最需要解决的问题就是链表边界和相加进位的问题。我的做法算是简单的对特殊情况打补丁。
[Leetcode Week15] 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] 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 ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
随机推荐
- Visual C++中对运行时库的支持
原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 一.什么是C运行时库 1)C运行时库就是 C run-time library,是 ...
- Oracle-RAC原理
Oracle-RAC原理 来源 https://blog.csdn.net/qq_34556414/article/details/79001267 单点数据库 VS RAC 单节点数据库,如果实例宕 ...
- 蒟蒻Orion还要学的东西!
这个ID多元化真是个麻烦的事情...... 一会KamijouIndex一会dedicatus545一会Orion的,乱死了啊啊啊啊 数据结构 圆方树 ETT 仙人掌 可持久化树套树 数学 洲阁筛 m ...
- c++ linux 判断string是中文的 or 英文的 字符串。
#include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h&g ...
- vector去除重复的元素
vector<int> v; sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end());
- shell中的数值运算
By francis_hao Oct 2,2017 本文摘录自bash的man手册. 算数运算相关的形式 形式 含义 ((expression)) expression按照下面描述的算术 ...
- Extjs 动态修改gridPanel列头信息以及store数据的方法
1 /*******************************checkbox按钮 历史报警信息**************************************/ var check ...
- 我们自己写的solr查询的代码作为search项目中的dao
我们自己写的solr查询的代码作为search项目中的dao,但是启动时会报错: 其实就是说 searchServiceImpl 中我们 Autowired 的 SearchDao 类 spring ...
- Qt ------ 初始化构造函数参数,parent
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setup ...
- Ubuntu14.04-Python2.7-Virtualenv-Django1.9-MySQL完整环境配置
一.安装Ubuntu14.04LTS 1.下载了ubuntu14.04后用ultraISO写到硬盘镜像(U盘) 开机启动项改成U盘在前,安装. 清空分区,重新分配. /最少10G,我放了100G. 物 ...