add two numbers
来源:互联网 发布:燕山大学网络教育平台 编辑:程序博客网 时间:2024/06/17 01:10
题目网址:add two numbers
本题存在几个需要注意的地方:
(1)需要注意链表的进位问题,如果最后两个链表均为空,那我们需要判断是否存在进位问题,如果存在进位,那我们就必须产生一个新的节点,加入到新的节点的后面;
(2)链表是不一样长的时候,我们需要将已经提前结束的那个链表的节点的值设置为0,同样想加;
(3)这个链表的数字是倒序的存放的,反而简化了我们的操作,因为我们实现链表的位数分离的时候,先%10,再/10,这样实现单链表就是逆序的,之后会实现一个顺序的!
迭代写法
提交的代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *newlistnode = new ListNode(0);int x, y,carry=0;ListNode *p = l1;ListNode *q = l2;ListNode *cur = newlistnode;while (p || q){x = p != NULL ? p->val : 0;y = q != NULL ? q->val : 0;int sum = x + y + carry;cur->next =new ListNode(sum%10);carry = sum / 10;cur = cur->next;if(p)p = p->next;if(q)q = q->next;}if (carry != 0){cur->next = new ListNode(1);}return newlistnode->next; }};
自己实现main函数的代码:
#include<iostream>#include<algorithm>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 *newlistnode = new listnode(0);int x, y,carry=0;listnode *p = l1;listnode *q = l2;listnode *cur = newlistnode;while (p || q){x = p != NULL ? p->val : 0;//若链表已经走到最后,那么设置值为0,否则为那个节点的值y = q != NULL ? q->val : 0;int sum = x + y + carry;cur->next =new listnode(sum%10);carry = sum / 10;cur = cur->next;if(p)p = p->next;if(q)q = q->next;}if (carry != 0)//如果最后还存在进位,那么我们就创建一个新节点加入到最后{cur->next = new listnode(1);}return newlistnode->next;}listnode *createlist(int n){//为输入的数字创建单链表listnode *newlistnode = new listnode(0);listnode *cur = newlistnode;do{cur->next = new listnode(n % 10);n = n / 10;cur = cur->next;} while (n);return newlistnode->next;}void output(listnode *l){//实现单链表的输出操作listnode *cur = l;while (cur){cout << cur->val << " ";cur = cur->next;}cout << endl;}};int main(){int n, m;while (cin>>n>>m){solution s;listnode *l1 = s.createlist(n);listnode *l2 = s.createlist(m);listnode *newlistnode = s.addtwonumbers(l1, l2);s.output(newlistnode);}return 0;}
在看到网上的代码之后,有使用递归的方法!
递归写法
a.如果两个节点都为空的话,则返回进位的节点,要么为空,要么为新创建的进位节点;
b.如果其中一个为空,那么我就要使得那个为空的节点的val等于0,实现另一个节点值和进位的加和;
c.如果均不为空,那么我们就需要实现两个节点和carry的和
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { return rec(l1, l2, 0); } ListNode *rec(ListNode *l1, ListNode *l2, int carry){if (l1 == NULL&&l2 == NULL){return carry == 0 ? NULL : new ListNode(carry);}if (l1 == NULL&&l2 != NULL){l1 = new ListNode(0);}if (l2 == NULL&&l1 != NULL){l2 = new ListNode(0);}int sum = l1->val + l2->val + carry;ListNode *cur = new ListNode(sum % 10);cur->next = rec(l1->next, l2->next, sum / 10);return cur;}};只是粘贴了提交部分的代码,如果需要整个main函数,直接套用上面的就行!之后再补上如果正序安放的数字的博客!
阅读全文
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
- Minmum Depth of Binary Tree
- 谁是真凶?《CSI:犯罪现场调查》正帮助AI提高断案能力
- Uber开源深度概率编程语言Pyro,AI实验室蛰伏一年首现身
- 何小鹏:从工程师思维到用户思维,这是互联网造车将带来的变革
- 机器学习基石-06-4-A Pictorial Proof
- add two numbers
- IOS获取设备唯一标识的八种方法
- C语言进阶-6讲: 递归法问题求解
- PAT (Basic Level) Practise (中文)1019. 数字黑洞 (20)
- tomcat查看GC信息
- vs2015 qt生成的exe文件没有图标(添加到控件上的图标,而不是.exe的图标)
- Rootkit实战——ls篇
- 手动实现简单的神经网络(唐宇迪神经网络课程笔记)
- Ubuntu设置SSH免密登录(不同于CentOS)