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

两数相加


​ 最开始使用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));
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值