Add Two Numbers
来源:互联网 发布:matlab提取矩阵某一列 编辑:程序博客网 时间:2024/06/03 10:35
题目来源:
https://leetcode.com/problems/add-two-numbers/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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
心路历程:
这道题在leetcode上的难度标示为Medium,但感觉不是太难,主要考查结构体和链表的运用。其中有些特殊情况,是在提交之后报错才知道的。比如,要考虑最高位的进位。还有一个关键点,就是两个链表长度不一致时,要把比较短的那个链表高位用0来代替。
解决方案:
1.自己写的C++版解法,遇到的一个坑是,结构体等于NULL的时候,不能再指向下一个节点。(一开始天真地以为NULL的下一个节点还是NULL)
#include<iostream>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* head = new ListNode(0);ListNode* result = head;int flag = 0;int add1, add2, sum, rem;//加数1,加数2,和,余数int carry = 0;//进位while (l1 != NULL || l2 != NULL) {//只要任意一个链表不为空则继续if (l1 == NULL) add1 = 0; else add1 = l1->val;//空的链表赋值为0if (l2 == NULL) add2 = 0; else add2 = l2->val;sum = add1 + add2 + carry;carry = sum / 10;rem = sum % 10;if (flag == 0) { //链表头直接赋值head->val = rem;flag++;}else { //新建节点head->next = new ListNode(rem);head = head->next;}if (l1 != NULL)l1 = l1->next;if (l2 != NULL) l2 = l2->next;}if (carry == 0){}//处理最高位else {head->next = new ListNode(carry);head = head->next;}return result;}};int main() {ListNode* l1 = new ListNode(0);ListNode* l2 = new ListNode(0);ListNode* arg1 = l1;ListNode* arg2 = l2;l1->val = 2;l1->next = new ListNode(4);l1 = l1->next;l1->next = new ListNode(3);l2->val = 5;l2->next = new ListNode(6);l2 = l2->next;l2->next = new ListNode(4);Solution solution;ListNode* result = solution.addTwoNumbers(arg1, arg2);while (result != NULL) {cout << result->val;result = result->next;}cout << endl;system("pause");}2.第二种解决方案来自leetcode,用Java写的,思路与第一种基本一致。主要的优化是采用了虚拟链表头,使得循环时不需要进行一次判断,最后返回链表头的下一个节点即可。这种思路值得借鉴。代码如下:
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); ListNode p = l1, q = l2, curr = dummyHead; int carry = 0; while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q != null) ? q.val : 0; int sum = carry + x + y; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (p != null) p = p.next; if (q != null) q = q.next; } if (carry > 0) { curr.next = new ListNode(carry); } return dummyHead.next;
阅读全文
0 0
- Add two numbers & Add binary
- LeetCode: Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode4: Add Two Numbers
- Leetcode : Add Two Numbers
- Add Two Numbers
- 【leetcode】Add Two Numbers
- add two numbers
- Add Two Numbers
- Add Two Numbers
- STM32中FSMC与硬件I2C冲突
- 对象家族后续之单例模式
- 导航条显示隐藏问题(包含改变状态栏颜色)
- hdu 6212 区间dp
- 算术表达式(栈)
- Add Two Numbers
- svg初学——渐变
- 安卓开发setContentView
- JavaScript中String、Number以及Boolean三种基本类型的特殊用法
- CSDN 开始起航
- OKhttp
- 【书摘】三毛最深情的二十句话
- 数据库中的五种约束及其添加方法
- MySQL 获得当前日期时间(以及时间的转换)