OJ 002 Add Two Numbers
来源:互联网 发布:淘宝助理怎么上架商品 编辑:程序博客网 时间:2024/06/05 00:30
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
首先想到的是用递归来完成这个问题。
代码:
class Solution {public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *ret = NULL; cout << "使用递归完成" << endl; ret = addTwoPoint(l1, l2, 0); return ret; } ListNode *addTwoPoint(ListNode *l1, ListNode *l2, int carry) { /*********************** 参数:l1第一条链表 l2第二条链表 carry进位 ***********************/ if (l1 == NULL&&l2 == NULL) { if (carry == 0) return NULL; else{ ListNode *npoint = new ListNode(carry); return npoint; } } else if (l1 == NULL&&l2 != NULL) { int tmp = l2->val + carry; ListNode *npoint = new ListNode(tmp % 10); npoint->next = addTwoPoint(l1, l2->next, tmp / 10); return npoint; } else if (l1 != NULL&&l2 == NULL) { int tmp = l1->val + carry; ListNode *npoint = new ListNode(tmp % 10); npoint->next = addTwoPoint(l1->next, l2, tmp / 10); return npoint; } else { int tmp = l1->val + l2->val + carry; ListNode *npoint = new ListNode(tmp % 10); npoint->next = addTwoPoint(l1->next, l2->next, tmp / 10); return npoint; } }};
上面是使用递归进行计算的代码,很简单,那如果改成循环做,该如何做呢?代码如下:
class Solution {public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { /* 使用循环完成 */ //cout << "使用循环完成" << endl; ListNode *ret = new ListNode(-1); int carry = 0; ListNode *listA = l1; ListNode *listB = l2; ListNode *tmp = ret; int sum = 0; while (listA != NULL&&listB != NULL) { sum = listA->val + listB->val + sum; tmp->next = new ListNode(sum % 10); sum = sum / 10; listA = listA->next; listB = listB->next; tmp = tmp->next; } while (listA != NULL) { sum = listA->val + sum; tmp->next = new ListNode(sum % 10); sum = sum / 10; listA = listA->next; tmp = tmp->next; } while (listB != NULL) { sum = listB->val + sum; tmp->next = new ListNode(sum % 10); sum = sum / 10; listB = listB->next; tmp = tmp->next; } if (sum != 0) { tmp->next = new ListNode(sum % 10); } ret = ret->next; //去掉第一个无用节点,如果严格来说,应该释放这块内存 return ret; }};
假如我们修改一下题目,让最高位在链表的开头,然后要怎么做呢?
代码:
class Solution {public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { int len1 = 0; int len2 = 0; ListNode *t1 = l1; ListNode *t2 = l2; while (t1 != NULL) { t1 = t1->next; len1++; } while (t2 != NULL) { t2 = t2->next; len2++; } if (len1 < len2) { return addTwoNumbers(l2, l1); } int step = len1 - len2; t1 = l1; t2 = l2; ListNode *ret = new ListNode(0); if (step == 0) { // ret->next = t1; int carry = addTwoPoint(t1, t2); ret->val = carry; if (carry == 0) ret = ret->next; return ret; } while (step > 1) { t1 = t1->next; step--; } ret = l1; int carry = addTwoPoint(t1->next, t2); t1->val += carry; return ret; } int addTwoPoint(ListNode *l1, ListNode *l2) { /*********************** 参数:l1第一条链表 l2第二条链表 carry进位 ***********************/ int carry; if (l1->next == NULL) { carry = 0; } else { carry = addTwoPoint(l1->next, l2->next); } int sum = l1->val + l2->val + carry; l1->val = sum % 10; return sum / 10; }};
0 0
- OJ 002 Add Two Numbers
- [LeetCode OJ][002]Add Two Numbers
- 【LeetCode OJ 002】Add Two Numbers
- LeetCode OJ:Add Two Numbers
- LeetCode OJ - Add Two Numbers
- LeetCode OJ Add Two Numbers
- LeetCode OJ - Add two Numbers
- LeetCode OJ ----Add Two Numbers
- LeetCode OJ:Add Two Numbers
- Leetcode OJ:Add Two Numbers
- 002Add Two Numbers
- 002 - Add Two Numbers
- 002 Add Two Numbers
- 002Add-Two-Numbers
- LeetCode OJ #2 Add Two Numbers
- LeetCode OJ 02 Add Two Numbers
- LeetCode OJ : 2 Add Two Numbers
- <LeetCode OJ> 2. Add Two Numbers
- 黑马程序员——OC三大特性----封装、继承、多态
- Leetcode2: Rotate Array
- 【STL】 next_permutation用法
- Array.forEach() & Using square brackets to obtain the value of a property
- 排序算法--计数排序(java)
- OJ 002 Add Two Numbers
- Mac应用程序提升为管理员权限的方法
- STL/next_permutation()和prev_permutation()函数
- 本地tomcat加载js没问题,但是放到远程上就报404
- PHP开发中的乱码问题
- 队列 广搜 nyoj 58 最少步数
- maven3 手动安装本地jar到仓库
- linux下logrotate 配置和理解
- A Guided Tour of WPF By Josh Smith