LeetCode题解之Add two numbers
1、题目描述
2、题目描述
题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 。
3、代码
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode( ) ;
ListNode* result = head;
ListNode* p1 = l1;
ListNode* p2 = l2;
int up = ;
while( p1 != NULL && p2 != NULL )
{
int r = p1->val + p2->val ;
r += up;
if( r < )
{
result->next = new ListNode( r ) ;
up = ;
}
else
{
result -> next = new ListNode( r -) ;
up = ;
}
p1 = p1->next;
p2 = p2->next;
result = result ->next;
} while( p1 != NULL )
{
if( p1->val + up < )
{
result->next = new ListNode( p1->val + up );
up = ;
}else{
result->next = new ListNode( p1->val + up - );
up = ;
}
p1 = p1->next;
result = result ->next;
} while( p2 != NULL )
{
if(p2->val + up < )
{
result->next = new ListNode( p2->val + up);
up = ;
}
else
{
result->next = new ListNode( p2->val + up - ) ;
up = ;
}
p2 = p2->next;
result = result->next;
} if( up != && p1 == NULL && p2 == NULL )
{
result->next = new ListNode(up) ;
}
return head->next; }
LeetCode题解之Add two numbers的更多相关文章
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode题解2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- LeetCode题解 #2 Add Two Numbers
题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
- LeetCode 题解之Add Two Numbers II
1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...
随机推荐
- jQuery表格自动增加
<!DOCTYPE html> <html dir="ltr" lang="zh-CN"> <head> <meta ...
- 【C#小知识】C#中一些易混淆概念总结(八)---------解析接口 分类: C# 2014-02-18 00:09 2336人阅读 评论(4) 收藏
这一篇主要来解析关于面向对象中最总要的一个概念--接口. 对于接口来说,C#是有规定使用Interface关键字来声明接口.它的声明是和类一致的.可以说接口就是一个特殊的抽象类.如下代码: cl ...
- 《Android应用性能优化》3——电量、渲染
7.延长电池续航时间 尽管应用有时看起来没做多少事,但实际上可能会很耗电,运行不了多久就会把电量榨干,使设备开机不到半天就没电了.那些被归为“电池杀手”的应用,最终的宿命就是被删除.得差评.不挣钱.因 ...
- facebook 摘要生成阅读笔记(一) A Neural Attention Model for Sentence Summarization
流程: 1.文本和摘要全部输入到模型中. 2.训练时,对生成摘要取前C个词,从头开始取,如果生成的摘要不足C,那么不足的地方直接补<s>. 3.训练时,最大化生成的摘要与原摘要的概率,即每 ...
- 解释mysql 语句 ——解释CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
在我们创建mysql数据库的时候我们经常会用到这句SQL:CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ ...
- 判断产品Key的正则表达式(格式: ABCD1-ABCD2-ABCD3-ABCD4-ABCD5)
正则表达式: ^[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}$ 改进: ^([A-Za-z0-9 ...
- The number of object passed must be even but was [1]
1.失败,使用TransportClient public static void bulkInsert(TransportClient client) throws IOException { Li ...
- nodeJs的npm报错问题
1. Failed at the phantomjs-prebuilt@2.1.14 install script 'node install.js'. 解决办法: npm install phant ...
- RabbitMQ 上手记录-part 1-基础概念
RabbitMQ是什么,不用多介绍了,毕竟名声在那,江湖地位摆着,搜索引擎收录着.为什么突然去学习这个框架了,毕竟工作中没有用得上(说来也惭愧,工作中开发的项目没有使用这个框架).但是作为互联网分布式 ...
- Python socket编程之构造IP首部和ICMP首部
这两天在做一个实验需要自己构造IP首部,遇到诸多问题,搞了一天终于搞定. 关于socket的介绍网上一大堆,我只记录构造IP头时我遇到的问题.由于没玩过socket构造IP首部,网上找了段代码研究下, ...