[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 ...
随机推荐
- Redis 学习之主从复制
该文使用centos6.5 64位 redis3.2.8 主从复制 Redis的复制功能是支持多个数据库之间的数据同步.一类是主数据库(master)一类是从数据库(slave),主数据库可以进 ...
- MyBatis原理简介
1.什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyB ...
- BZOJ 1103 大都市(dfs序+树状数组)
应该是一道很水的题吧... 显然可以用树链剖分解决这个问题,虽然不知道多一个log会不会T.但是由于问题的特殊性. 每次修改都是将边权为1的边修改为0,且询问的是点i到根节点的路径长度. 令点i到根节 ...
- C++中Set的使用
/* #include <string> // 使用 string 类时须包含这个文件 #include <iostream> // 这个就加上去吧.c++的输入和输出. us ...
- POJ3648:Wedding——题解(配2-SAT简易讲解)
http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...
- BZOJ2875 & 洛谷2044:[NOI2012]随机数生成器——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2875 https://www.luogu.org/problemnew/show/P2044 栋栋 ...
- 洛谷 P1291 [SHOI2002]百事世界杯之旅 解题报告
P1291 [SHOI2002]百事世界杯之旅 题目描述 "--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...
- 全面解析JavaScript的Backbone.js框架中的Router路由
这篇文章主要介绍了Backbone.js框架中的Router路由功能,Router在Backbone中相当于一个MVC框架中的Controller控制器功能,需要的朋友可以参考下. Backbone ...
- Mysql 语句优化技巧
前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...
- 后端日期类属性date 不接受string类型日期,都是没找到解决的方法,所有前端传回的string字符串都一一转化为java定义的类型
1.比如日期 我们可以是yyyy-MM-dd 亦可以是 yyyy-MM-dd HH:mm:ss 方法1在java代码中需要的字段上加上注解 写上日期类型,不过这样很麻烦,每个人写了日期类型的接收前端的 ...