当前位置:首页 > 算法 > 正文内容

(LeetCode刷题)2. 两数相加

chanra1n1个月前 (03-19)算法94

题目

image.png

image.png

解答一:


简单实现思路:先遍历完两个链表,把各自的数字存入两个数组,然后对应位置的数相加,若结果大于10就进位到更高位的数。

/**
 * Definition for singly-linked list->
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *head = NULL, *tail = NULL;
    int carry = 0;
    while (l1 || l2) {
        int n1 = l1 ? l1->val : 0;
        int n2 = l2 ? l2->val : 0;
        int sum = n1 + n2 + carry;
        if (!head) {
            head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
            tail->val = sum % 10;
            tail->next = NULL;
        } else {
            tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
            tail->next->val = sum % 10;
            tail->next->next = NULL;
            tail = tail->next;
        }
        carry = sum / 10;
        if (l1) {
            l1 = l1->next;
        }
        if (l2) {
            l2 = l2->next;
        }
    }
    if (carry > 0) {
        tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        tail->next->val = carry;
        tail->next->next = NULL;
    }
    return head;
}

image.png

算法的时间复杂度就是O(N)。因为不论怎样,只要能够找到一个答案,你都必须遍历所有的数字,无法再进一步精简遍历的次数。

解答二:

不过,我们同样可以用O(N)的时间复杂度,但是可以减少代码的复杂性和提高运行的效率。我们可以使用以下策略:边遍历链表边进行加法计算并生成新的节点,然后利用一个carry变量存储进位的值,这样可以避免多次遍历和额外的存储空间。例如:

/**
 * Definition for singly-linked list->
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *head = NULL, *tail = NULL;
    int carry = 0;
    while (l1 || l2 || carry) {
        int sum = carry;
        if (l1) {
            sum += l1->val;
            l1 = l1->next;
        }
        if (l2) {
            sum += l2->val;
            l2 = l2->next;
        }
        struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode));
        node->val = sum % 10;
        node->next = NULL;
        carry = sum / 10;
        if (!head) {
            head = node;
        } else {
            tail->next = node;
        }
        tail = node;
    }
    return head;
}

image.png



扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:https://www.myfpga.cn/index.php/post/394.html

分享给朋友:

“(LeetCode刷题)2. 两数相加” 的相关文章

​Fibonacci数列的初步实现-递归

​Fibonacci数列的初步实现-递归

Fibonacci数列为:1、1、2、3、5、8、13、21、…,即 Fibonacci(1)=1; Fibonacci(2)=1;Fibonacci(n)=Fibonacci(n-1)+ Fibonacci(n-2)(当n>2时)。#include <stdio.h>...

常见算法的C语言实现(带题目、分析和答案) 穷举 递归 迭代 递推 分治 回溯 动态规划 贪心

常见算法的C语言实现(带题目、分析和答案) 穷举 递归 迭代 递推 分治 回溯 动态规划 贪心

1.1   基本思想1.1.1  穷举穷举(又称枚举)算法的基本思想是从所有可能的情况中搜索正确的答案。a)      题一查找数组中的两个元素,它们的和等于给定的目标值。给定一个包含 n 个整数的数组和一个目标值,找出...

(LeetCode刷题)1. 两数之和

(LeetCode刷题)1. 两数之和

题目解答一:/**  * Note: The returned array must be malloced, assume caller calls free(). &nbs...

(LeetCode刷题)3. 无重复字符的最长子串

(LeetCode刷题)3. 无重复字符的最长子串

题目:解法一:class Solution(object):     def lengthOfLongestSubstring(self,s):        &nb...