LeetCode刷题日记(2. 两数相加)

2.两数相加

答题思路:

使用两个指针对输入的链表进行遍历,每次遍历后将结果保存在数组中,最后将数组中的值传入新建链表中。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode *p1 = l1, *p2 = l2;
    int res[101] = { 0 };  // 保存结果的数组
    int N = 0;   // 结果数组的长度or索引
    int temp;    // 两数相加的临时结果
    int ex = 0;  // 进位
    while (p1 || p2) {
        // l1中的所有数都已经加完了
        if (p1 == NULL) {
            temp = p2->val + ex;
            ex = temp > 9 ? 1 : 0;
            res[N++] = temp > 9 ? (temp-10) : temp;
            p2 = p2->next;
        }
        // l2中的所有数都已经加完了
        else if (p2 == NULL) {
            temp = p1->val + ex;
            ex = temp > 9 ? 1 : 0;
            res[N++] = temp > 9 ? (temp-10) : temp;
            p1 = p1->next;
        }
        // l1、l2中的所有数都还没加完
        else {
            temp = p1->val + p2->val + ex;
            ex = temp > 9 ? 1 : 0;
            res[N++] = temp > 9 ? (temp-10) : temp;
            p1 = p1->next;
            p2 = p2->next;
        }
    }
    // 如果加到最后进位为1,需要将1写在最后
    if (ex) {
        res[N++] = ex;
    }
    // 将数组中的结果保存在链表中
    int i;
    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->val = 0;
    head->next = NULL;
    struct ListNode *p0 = head;
    for (i=0;i<N;i++) {
        struct ListNode *pt = (struct ListNode*)malloc(sizeof(struct ListNode));
        pt->next = NULL;
        pt->val = res[i];
        p0->next = pt;
        p0 = pt;
    }
    return head->next;
}

错误记录:

Line 70: Char 15: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [ListNode.c] 0xbebebebebebebebe: note: pointer points here <memory cannot be printed>

原因:链表最后一个节点的next指针需要赋值为NULL,否则不知道指向位置。

改进空间:

1、加法计算可以通过函数简化写法

2、可以将数组变量取消掉,直接将结果保存到链表中,最后再返回

别人家的代码(看完之后我觉得我写的都是什么垃圾代码)

#include <stdlib.h>

struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{
    struct ListNode *result = calloc(1, sizeof(struct ListNode));
    struct ListNode *current = result;
    for (;;)
    {
        if (l1)
        {
            current->val += l1->val;
            l1 = l1->next;
        }
        if (l2)
        {
            current->val += l2->val;
            l2 = l2->next;
        }

        if (current->val >= 10)
        {
            current->next = calloc(1, sizeof(struct ListNode));
            current->next->val = 1;
            current->val -= 10;
            current = current->next;
            continue;
        }

        if (!l1 && !l2)
            break;

        current->next = calloc(1, sizeof(struct ListNode));
        current = current->next;
    }

    return result;
}

可以称得上是诗一样的代码了!

没有多余的变量,代码简洁明了,我不禁想,为什么我写不出这么优雅的代码?

我总结了一些原因:

1、只顾着完成功能,没有花时间雕琢

2、对C语言的语法、特性的了解较浅

仅仅把题目做出来、做对就行了,这种想法或许在学生时代没有问题,但是要想实现自我提升,还必须要将写代码像写诗一样对待,保持对代码的热爱,对算法的激情,经过不断的打磨雕琢,或许才能写出像诗一样的代码吧!优秀的代码应该是赏心悦目的,而不是一团乱糟糟的程序,我以后要像优秀的代码看齐!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值