leetcode题解2. Add Two Numbers
2024-10-15 02:54:02
题目:
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.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807. 首先先来分析一下题目
这个题目我的思路就是把这两个链表都转换成数字,然后相加,再将获得的和进行转化,转化为我们需要的链表结果。
看起来是个很完美的想法,不过很久没写代码还是发现了一些问题。
没有在IDE上写直接在leetcode上写报错了,
Line 20: request for member 'val' in 'l1', which is of pointer type 'ListNode*' (maybe you meant to use '->' ?报了这个错误,还是对结构体和指针的研究不够深入不知道什么时候用->什么时候用.
百度上搜到一个不错的答案:
->是指针指向其成员的运算符
.是结构体的成员运算符。
最大的区别是->前面放的是指针,而.前面跟的是结构体变量。
例如:struct
A
{
int
a;
int
b;
};
A *point =
malloc
(
sizeof
(
struct
A));
point->a = 1;
A object;
object.a = 1;
然后附上我这个思路的代码:
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
long long num1=0;
long long num2=0;
long long k=1;
long long ans=0;
ListNode* l3;
ListNode* temp;
while(true)
{
num1=(l1->val)*k+num1;
k=k*10;
if(l1->next==NULL) break;
l1=l1->next;
}
k=1;
while(true)
{
num2=l2->val*k+num2;
k=k*10;
if(l2->next==NULL) break;
l2=l2->next;
}
ans=num1+num2;
temp=new ListNode(0);
l3=temp;
while(true)
{
temp->val=ans%10;
ans=ans/10;
if(ans==0) break;
temp->next=new ListNode(0);
temp=temp->next;
}
return l3;
}
};
这个方法并不能完全过数据,显然我忽略了一种样本数据很长的情况,从int换成long long也装不下这么多的数据。
所以需要换一个方法。
看了一下网上的和官方的解答思路,也就是需要将同一位的两个node里面的值加起来
当然也要考虑进位的问题,进位方面还有比较重要的就是如果最高位不够的情况要新增一个结点来存放最高位的1。
现在就展示一下后来的正确解法的代码部分:
/**
* 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* l3=new ListNode(0);
ListNode* temp=l3;
int carry=0;
while(l1!=NULL||l2!=NULL)
{
int n1=0;
int n2=0;
if(l1!=NULL) n1=l1->val;
else n1=0;
if(l2!=NULL) n2=l2->val;
else n2=0;
int n3=n1+n2+carry; //sum is num1 and num2 and carry!
carry=n3/10;
n3=n3%10;
if(temp!=NULL)
{
temp->next=new ListNode(n3);
}
temp=temp->next;
if(l1!=NULL) l1=l1->next;
if(l2!=NULL) l2=l2->next;
}
if(carry!=0)
{
temp->next=new ListNode(carry);
}
return l3->next;
}
};
这道题也让我复习了一下链表,学会很好的使用链表是很必要的
学习建立一个简单的链表
struct ListNode{
int val;
ListNode *next;
ListNode(int x) :val(x),next(NULL){}
};
最新文章
- JSTREE 实现AJAX重载入时刷新所有节点树
- PreparedStatement的应用
- CentOS Linux解决Device eth0 does not seem to be present
- jQuery晦涩的底层工具方法们
- Scalaz(7)- typeclass:Applicative-idomatic function application
- Android 自定义title 之Action Bar
- 【转】说说如何使用unity Vs来进行断点调试
- T4 模板的调试方法,方便大家遇到问题自己快速定位和优化
- MySQL双主配置
- POJ 1552 Doubles (C++ STL set使用)
- 谷歌的C++智能指针实现
- android基础4——Mainifest
- maven相关的学习资料
- 新的表格展示利器 Bootstrap Table
- TensorBoard使用
- LeetCode算法题-Shortest Completing Word(Java实现)
- Android support 26.0.0-alpha1 产生的问题(zz)
- ssm框架整合shiro
- LaTeX表格绘制备忘之Go语言中的几个表
- 算法笔记(c++)--使用一个辅助栈排列另一个栈