当前位置:   article > 正文

两数相加的题型解法(包含流程图分析以及测试用例)_输入两个数,并求两个数的和。 根据题意,画出流程图,并写出程序。

输入两个数,并求两个数的和。 根据题意,画出流程图,并写出程序。

一、题目需求

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
  • 1
  • 2
  • 3

二、解法

public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    if (l1 == null) {
        return l2;
    }
    if (l2 == null) {
        return l1;
    }
    ListNode head = new ListNode(-1);
    ListNode pre = head;
    int carry = 0;
    while (l1 != null && l2 != null) {
        int number = l1.val + l2.val + carry;
        carry = number / 10;
        pre.next = new ListNode(number % 10);
        pre = pre.next;
        l1 = l1.next;
        l2 = l2.next;
    }
    while (l1 != null) {
        int number = l1.val + carry;
        carry = number / 10;
        pre.next = new ListNode(number % 10);
        pre = pre.next;
        l1 = l1.next;
    }
    while (l2 != null) {
        int number = l2.val + carry;
        carry = number / 10;
        pre.next = new ListNode(number % 10);
        pre = pre.next;
        l2 = l2.next;
    }
    if (carry != 0) {
        pre.next = new ListNode(carry);
    }
    return head.next;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

三、测试用例

public class Test {
    
    public static void main(String[] args) {

        // 空链表测试
        ListNode empty1 = null;
        ListNode empty2 = null;
        ListNode empty = Solution.addTwoNumbers(empty1, empty2);
        show(empty);

        // 一个空链表测试
        ListNode oneEmpty = null;
        ListNode l = new ListNode(2);
        l.next = new ListNode(4);
        l.next.next = new ListNode(3);
        ListNode one = Solution.addTwoNumbers(oneEmpty, l);
        show(one);

        // 正常链表测试1
        ListNode l1 = new ListNode(2);
        l1.next = new ListNode(4);
        l1.next.next = new ListNode(3);

        ListNode l2 = new ListNode(5);
        l2.next = new ListNode(6);
        l2.next.next = new ListNode(4);

        ListNode node = Solution.addTwoNumbers(l1, l2);
        show(node);

        // 正常链表测试2
        ListNode l3 = new ListNode(9);
        l3.next = new ListNode(9);
        l3.next.next = new ListNode(9);
        l3.next.next.next = new ListNode(9);

        ListNode l4 = new ListNode(9);
        l4.next = new ListNode(9);
        l4.next.next = new ListNode(9);
        l4.next.next.next = new ListNode(9);
        l4.next.next.next.next = new ListNode(9);
        l4.next.next.next.next.next = new ListNode(9);

        ListNode node2 = Solution.addTwoNumbers(l1, l2);
        show(node2);
    }

    private static void show(ListNode node) {
        System.out.println("输出结果:");
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

四、流程图

1、第一个数开始相加
在这里插入图片描述

2、第二个数开始相加,发生进位

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jh2S7bzQ-1603713700791)(C:\Users\huang\AppData\Roaming\Typora\typora-user-images\image-20201026195718169.png)]

3、计算最后一位数

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D9xVposs-1603713700794)(C:\Users\huang\AppData\Roaming\Typora\typora-user-images\image-20201026195936687.png)]

4、因为carry为0,计算结束

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-use2Kwhe-1603713700795)(C:\Users\huang\AppData\Roaming\Typora\typora-user-images\image-20201026200027471.png)]

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号