0
点赞
收藏
分享

微信扫一扫

leetcode 2.两数相加 python + c++ + java

梦幻之云 2022-02-08 阅读 33

两数相加

文章目录


​ 最开始使用python,尝试使用列表解题,但这显然是不对的,提供的是一个链表结构。尝试失败后,因为学习数据结构的部分时使用c++语言进行学习的,故尝试使用c++进行解题,与python、java解题的最大不同是对于内存的分配和释放,这点需要进行注意。总共提供三种语言的代码,总体思路类似,具体实现有略微差别,java代码使用递归的方法,是最简洁的。

1.c++

以下是c++的第一次解题代码(malloc存在问题):

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
		struct ListNode *head = NULL, *tail = NULL;
	    int carry = 0;
	    while (l1 || l2) {
	    //位上有数值就赋值,没有赋值为0.直到没有值为止。
	        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 = tail->next;
	            tail->next = NULL;
	        }
	        //进位数
	        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;
    }
};

在内存分配方面,使用malloc分配内存本地可运行,在leetcode上会报错,需要将使用new方法进行分配,以下是改进版:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
		struct ListNode *head = NULL, *tail = NULL;
	    int carry = 0;
	    while (l1 || l2) {
	    //位上有数值就赋值,没有赋值为0.直到没有值为止。
	        int n1 = l1 ? l1->val : 0;
	        int n2 = l2 ? l2->val : 0;
	        int sum = n1 + n2 + carry;
	        if (!head) {
	        //动态分配内存
	            head = tail = new struct ListNode;
	            tail->val = sum % 10;
	            tail->next = NULL;
	        } else {
	            tail->next = new struct ListNode;
	            tail->next->val = sum % 10;
	            tail = tail->next;
	            tail->next = NULL;
	        }
	        //进位数
	        carry = sum / 10;
	        if (l1) { l1 = l1->next; }
	        if (l2) { l2 = l2->next; }
	    }
	    if (carry > 0) {
	        tail->next = new struct ListNode;
	        tail->next->val = carry;
	        tail->next->next = NULL;
	    }
	    return head;
    }
};

2.python

以下是python的解题代码:

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        #* 进位标记数
        carry = 0
        #* 创建结果链表表头
        ans = ListNode(0)
        #* 浮动索引 保持原始索引位置 方便调用链表头返回答案
        fans = ans
        while (l1 or l2):
            x = l1.val if l1 else 0
            y = l2.val if l2 else 0
            temp = x + y + carry
            carry = temp // 10
            fans.next = ListNode(temp%10)
            fans = fans.next
            if l1!=None: l1 = l1.next
            if l2!=None: l2 = l2.next
        if carry == 1:
            fans.next = ListNode(1)
        return ans.next

3.java

以下是Java的解题代码:

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        return newaddTwoNumbers(l1, l2,0);
    }
    public ListNode newaddTwoNumbers(ListNode l1, ListNode l2,int carry){
        // 处理最高位进位 carry:进位标记符,取值为0和1
        if(l1 == null && l2 == null){ return carry == 0?null:new ListNode(carry); }
        // 取值 + 移动游标
        if(l1 != null){ carry += l1.val; l1 = l1.next; }
        if(l2 != null){ carry += l2.val; l2 = l2.next; }
        // 迭代计算 传参(当前位置的计算结果,下一位的指针(l1,l2,进位标记符))
        return new ListNode(carry%10,newaddTwoNumbers(l1, l2, carry/10));
    }
}

举报

相关推荐

0 条评论