资讯专栏INFORMATION COLUMN

每日一则 LeetCode: Add Two Numbers

hightopo / 2510人阅读

摘要:描述中文解释给定两个非空的链表里面分别包含不等数量的正整数,每一个节点都包含一个正整数,肯能是,但是不会是这种情况。我们需要按照倒序计算他们的和然后再次倒序输出。

描述

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
中文解释

给定两个非空的链表里面分别包含不等数量的正整数,每一个节点都包含一个正整数,肯能是0,但是不会是01这种情况。我们需要按照倒序计算他们的和然后再次倒序输出。

解题思路

这题没有什么巧妙的方式,不过仔细思考一下,它其实是在模拟正常的多位数加法。我们试想在计算多位数加法的时候,从最末位开始计算,如果大于10就进位,并加到下次高位计算中;如果不大于10继续计算;就这样我们就有了下面的阶梯思路。
一次循环就可以搞定,通过判断他们其中是不是空,就像是多位数加减法,如果一个高位没有了,当然也要继续计算,所以有了下面默认 int carry = 0,然后通过 sum / 10 算出进位,通过 sum % 10 算出当前位,这个题就迎刃而解。

源码
public class AddTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode tempNode = new ListNode(0);
        ListNode a = l1, b = l2, curr = tempNode;
        int carry = 0;
        while (a != null || b != null) {
            int x = a != null ? a.val : 0;
            int y = b != null ? b.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (a != null) a = a.next;
            if (b != null) b = b.next;
        }

        if (carry > 0) {
            curr.next = new ListNode(carry);
        }

        return tempNode.next;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(2);
        l1.add(new ListNode(4));
        l1.add(new ListNode(3));
        ListNode l2 = new ListNode(5);
        l2.add(new ListNode(6));
        l2.add(new ListNode(4));
        ListNode listNode = new AddTwoNumbers().addTwoNumbers(l1, l2);
        System.out.println(listNode.val);
        while (listNode.next != null) {
            System.out.println(listNode.next.val);
            listNode = listNode.next;
        }
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    public void add(ListNode next) {
        ListNode last = getLast(this);
        last.next = next;
    }

    private ListNode getLast(ListNode next) {
        while (next.next != null) {
            return getLast(next.next);
        }
        return next;
    }
}
原题地址

https://leetcode.com/problems...

作者

本文作者麻酱,欢迎讨论,指正和转载,转载请注明出处。
如果兴趣可以关注作者微信订阅号:码匠笔记

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69348.html

相关文章

  • Leetcode 2 Add Two Numbers 两数相加

    摘要:这题是说给出两个链表每个链表代表一个多位整数个位在前比如代表着求这两个链表代表的整数之和同样以倒序的链表表示难度这个题目就是模拟人手算加法的过程需要记录进位每次把对应位置两个节点如果一个走到头了就只算其中一个的值加上进位值 Add Two Numbers You are given two linked lists representing two non-negative num...

    Charlie_Jade 评论0 收藏0
  • [LeetCode] 445. Add Two Numbers II

    Problem You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and ...

    alexnevsky 评论0 收藏0
  • leetcode 2 Add Two Numbers

    摘要:我们的目的是求出两个数字的加和,并以同样的形式返回。假设每个都不会存在在首位的,除非数字本身就是想法这道题主要要求还是熟悉的操作。这道题由于数字反序,所以实际上从首位开始相加正好符合我们笔算的时候的顺序。 题目详情 You are given two non-empty linked lists representing two non-negative integers. The d...

    Integ 评论0 收藏0
  • leetcode445. Add Two Numbers II

    摘要:题目要求对以链表形式的两个整数进行累加计算。思路一链表转置链表形式跟非链表形式的最大区别在于我们无法根据下标来访问对应下标的元素。因此这里通过先将链表转置,再从左往右对每一位求和来进行累加。通过栈可以实现先进后出,即读取顺序的转置。 题目要求 You are given two non-empty linked lists representing two non-negative i...

    DoINsiSt 评论0 收藏0
  • LeetCode 2:两数相加 Add Two Numbers

    摘要:给出两个非空的链表用来表示两个非负的整数。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。需要考虑到两个链表长度不同时遍历方式链表遍历完成时最后一位是否需要进一位。 ​给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 ...

    diabloneo 评论0 收藏0

发表评论

0条评论

hightopo

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<