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 ...
随机推荐
- 【C语言】reverse_string(char * string)(递归)
递归reverse_string(char * string)性能. 逆转 原始字符串 更改 相反,打印出的. /* 编写一个函数reverse_string(char * string)(递归实现) ...
- 机器人操作系统 除了Android还有一个ROS(转)
你知道市面上的机器人都采用了哪些操作系统吗? 估计大多数人给出的答案就是 Android 了.从市面上的产品来看,基于 Android 系统开发的机器人确实是主流,但是还有一种操作系统却鲜为人知,它叫 ...
- i++与++i哪个效率更高
简单的比较前缀自增运算符和后缀自增运算符的效率是片面的, 因为存在很多因素影响这个问题的答案. 首先考虑内建数据类型的情况: 如果自增运算表达式的结果没有被使用, 而是仅仅简单地用于增加一元操作数, ...
- HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online
意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...
- 【Android】九宫格实现
第一步,布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- OBIEE SampleAppv406 自己主动启动配置
SampleApp 一个简短的引论: SampleApp这是一个一站式服务,几乎证明OBIEE不管顶的特征可想而知. 它安装了大量的应用(数据库,OBIEE,的Endeca.TimesTen的.Ess ...
- 迷宫的最短路径 (BFS)
N*M的迷宫,从起点到终点,求最短距离 宽度优先搜索按照距开始状态由近及远的顺序进行搜索,因此可以很容易的用来求最短路径,最少操作之类问题的答案. (可以构造成pair或者编码成int来表达状态) ...
- bigdata_hive_Issue of Vectorization on Parquet table
When Vectorization is turned on in Hive:set hive.vectorized.execution.enabled=true;If the involved t ...
- js中位运算的运用
原文:js中位运算的运用 我们可能很少在编程中用位运算,如果没深入学习,可能也很难理解.平时的数值运算,其实是要先转换成二进制再进行运算的,而位运算就是直接进行二进制运算,所以位运算的执行效率肯定是更 ...
- 提高你的Java代码质量吧:推荐在复杂字符串操作中使用正则表达式
一.分析 字符串的操作,诸如追加.合并.替换.倒序.分隔等,都是在编码过程中经常用到的,而且Java也提供了append.replace.reverse.split等方法来完成这些操作,它们使用起来 ...