小白被力扣第2题折磨的心路历程,2024年最新含爱奇艺,2024年最新小米,2024年最新腾讯,2024年最新阿里

由于采用的是带头结点(方便插值)的链表,所以最终返回res->next:

return res->next;

至此,本题得解。

第一次做题的源代码:

#include
#include
using namespace std;

struct ListNode
{
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
int bit1 = 0;
int bit2 = 0; // 统计位数bit
ListNode* temp1 = l1;
ListNode* temp2 = l2; // 防止移动头指针
while (temp1 != NULL)
{
bit1++;
temp1 = temp1->next;
}
while (temp2 != NULL)
{
bit2++;
temp2 = temp2->next;
}
//算出了两个数的位数bit1,bit2
int num1 = 0;
int num2 = 0; // 待逆序存储的数字
ListNode* temp11 = l1;
ListNode* temp22 = l2; // 防止移动头指针
for (int i = 0; i < bit1; i++)
{
num1 += temp11->val * pow(10, i);
temp11 = temp11->next;
}
for (int i = 0; i < bit2; i++)
{
num2 += temp22->val * pow(10, i);
temp22 = temp22->next;
}
int sum = num1 + num2; // 计算出最终存储的逆序前的数
int temp = sum;
int count = 1;
while (temp / 10 != 0) // 统计sum的位数
{
temp /= 10;
count++;
}
//count是最后和的位数,sum是最后和的值

ListNode* head = new ListNode;
head->val = NULL;
head->next = NULL;
ListNode* current = head; // 防止移动头指针

string sum_str = to_string(sum); // 将最后和结果转换为字符串,方便逆序存储

// 没有头节点,先对第一位进行操作,类似于初始化头节点
int digit_value = sum_str[count - 1] - ‘0’; // char型转化为int型需要减去’0’的ASCII码
current->val = digit_value;

// 再对头节点后的数进行操作
for (int i = count - 1; i > 0; i–)
{
ListNode* temp = new ListNode;
int digit_value = sum_str[i - 1] - ‘0’;
temp->val = digit_value;
current->next = temp;
current = current->next;
}
// 最后节点的指针指向空
current->next = NULL;
return head;
}

};

void test()
{
ListNode* l1 = new ListNode;
l1->val = 5;
ListNode* node12 = new ListNode;
node12->val = 6;
l1->next = node12;
ListNode* node13 = new ListNode;
node13->val = 4;
node12->next = node13;

ListNode* l2 = new ListNode;
l2->val = 2;
ListNode* node22 = new ListNode;
node22->val = 4;
l2->next = node22;
ListNode* node23 = new ListNode;
node23->val = 3;
node22->next = node23;

Solution s;
ListNode* res = s.addTwoNumbers(l1, l2);
cout << res->val << " " << res->next->val << " " << res->next->next->val << endl;

// 结果:可以满足题目要求,但是无法满足l1,l2中存放过大数据的情况,因为int类型能存储的位数有限,见39、44行
// 结论:我写的这种方法不适用
}

int main()
{
test();
system(“pause”);
}

第二次做题的源代码:

#include
#include
using namespace std;

struct ListNode
{
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
int sum = 0; // 两位总和
int data = 0;// 个位
int bit = 0; // 进位
ListNode* res; // 指针不会调用构造和析构函数,只是定义了一个指针,没有申请内存
res = new ListNode; // 使用new创建对象时,申请分配内存,调用构造函数
// 此时的res!=NULL,因为res内有val=0
// res->next为NULL
ListNode* current = res;
while (l1 != NULL && l2 != NULL)
{
ListNode* temp;
temp = new ListNode;
sum = l1->val + l2->val + bit; // 在这里加上进位
bit = sum / 10;
data = sum % 10;
temp->val = data;
current->next = temp;
l1 = l1->next;
l2 = l2->next;
current = current->next;
}
while (l1 != NULL)
{
ListNode* temp;
temp = new ListNode;
sum = l1->val + bit;
bit = sum / 10;
data = sum % 10;
temp->val = data;
current->next = temp;
l1 = l1->next;
current = current->next;
}
while (l2 != NULL)
{
ListNode* temp;
temp = new ListNode;
sum = l2->val + bit;
bit = sum / 10;
data = sum % 10;
temp->val = data;
current->next = temp;
l2 = l2->next;
current = current->next;
}
if (bit != 0)
{
ListNode* temp;
temp = new ListNode;
temp->val = bit;
current->next = temp;
current = current->next;
}
return res->next;
}
};

void test()
{
ListNode* l1 = new ListNode;
l1->val = 5;
ListNode* node12 = new ListNode;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
[外链图片转存中…(img-8iSnWik8-1712577718976)]

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值