leetcode第四题--Add Two Numbers
Problem:
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
一开始就是想到先把第一个list转换成一个数,然后把第二个转换成第二个数,然后相加后,再把相加的值变成list。代码如下
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int a = , b = , ans = , flag = ;
int index1 = , index2 = ;
ListNode *temp1, *temp2, *result;
temp1 = l1;
temp2 = l2;
while(temp1->next != NULL )
{
index1++;
a += (temp1 -> next -> val) * (int)pow(,index1);
}
while(temp2 -> next != NULL)
{
index2++;
b += temp2 ->next -> val * (int)pow(, index2);
}
ans = a + b;
result -> val = ans%;
result -> next = NULL;
flag = ans/;
while(flag)
{
ListNode *added = new ListNode(flag%10);
result ->next = added;
flag = flag/;
}
return result;
}
然后是Time Limit Exceed了。那就不能这样做,应该直接在链表相加。加到某个链表结束为止。要用中间变量记住当前的进位。如果最后进位不为零(也就是为1)的话,那还是需要记录的。代码贴出如下:
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode * ans = NULL, *last = NULL;
int up = ;
while (l1 != NULL && l2 != NULL)
{
int tmp = l1->val + l2->val + up;
up = tmp / ;
if (last == NULL)
{
ans = new ListNode(tmp % );
last = ans;
}
else
last = pushBack(last, tmp % );
l1 = l1->next;
l2 = l2->next;
}
while (l1 != NULL)
{
int tmp = l1->val + up;
last = pushBack(last, tmp % );
up = tmp / ;
l1 = l1->next;
}
while (l2 != NULL)
{
int tmp = l2->val + up;
last = pushBack(last, tmp % );
up = tmp / ;
l2 = l2->next;
}
if (up == )
{
ListNode * l = new ListNode(up);
last->next = l;
}
return ans;
} ListNode * pushBack(ListNode * last, int val)
{
ListNode * l = new ListNode(val);
last->next = l;
return l;
}
};
还是要感谢suool大神,改天一定要再做一次看看是不是真的掌握了。自己真的水平有限啊。不过只要肯努力,一天进步一点点就好。让cnblogs记录我的学习过程,come on!
leetcode第四题--Add Two Numbers的更多相关文章
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode解题笔记 - 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 linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- [算法题] Add Two Numbers
题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
随机推荐
- 【J2SE】java实现简单照片查看器
程序执行结果: project结构图: 程序代码: import java.awt.BorderLayout; import java.awt.FileDialog; import java.awt. ...
- Mesos-error
1,configure: error: cannot find libcurl 解决 yum install curl-devel 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- Catalan数总结
财产: 前20条目:1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, ...
- UnitOfWork应用
UnitOfWork以及其在ABP中的应用 Unit Of Work(UoW)模式在企业应用架构中被广泛使用,它能够将Domain Model中对象状态的变化收集起来,并在适当的时候在同一数据库连接和 ...
- 浅谈SQL注入风险 - 一个Login拿下Server(转)
前两天,带着学生们学习了简单的ASP.NET MVC,通过ADO.NET方式连接数据库,实现增删改查. 可能有一部分学生提前预习过,在我写登录SQL的时候,他们鄙视我说:“老师你这SQL有注入,随便都 ...
- rabbitMQ说明文档
rabbitMQ是什么 RabbitMQ 是由 LShift 提供的一个 Advanced Message Queuing Protocol (AMQP) 的开 ...
- 单点登录SSO原则的实现
单点登录SSO(Single Sign On)简单地把它是下一个在多系统环境中共存,在用户登录,不要在其他系统上的登录.这是用户第一次登录得到所有其他系统的信任. 单点登录在一个大型网站非常频繁使用, ...
- C#操作Xml:XSLT语法 在.net中使用XSLT转换xml文档示例
XSL即可扩展的样式表文件. 可以格式化xml的显示,也可以将xml转换成需要的另一种格式. 学习XSL必须熟悉XPath.XSL和XPath一样简单强大,容易学习. 1. XSL既然可以格式化xml ...
- Spring中的事务传播行为
Spring在TransactionDefinition接口中定义了7种类型的事务传播行为,它们规定了事务方法是怎样传播的. PROPAGATION_REQUIRED(最经常使用!):支持当前事务,假 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 SharePoint中基于Web开发
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 SharePoint中基于Web开发 之前提到过, ...