繁体   English   中英

将两个数字相加表示为图形

[英]Adding two numbers that are represented as graphs

我正在尝试在leetcode上解决此问题: https ://leetcode.com/problems/add-two-numbers/我的一般策略是:

  1. 添加每个列表的前几位
  2. 继续添加数字,直到一个节点(或两个节点)位于最后一个元素
  3. 继续从具有更多元素的列表中添加
  4. 返回值

我进行的一项测试是l1 = [1,8,6]l2 = [1,2,3] 我的答案是[0,0,0] ,正确的答案是[2,0,0,1]

我添加了注释以提高可读性。

/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        int ii = l1.val + l2.val;
        ListNode nd;
        if (ii >= 10){ // adding the first digits
            carry = ii / 10;
            nd = new ListNode(ii%10);
        } else {
            nd = new ListNode(ii);
        }
        ListNode lst = nd;
        // keep adding corresponding digits until one of the lists is about to end
        while (l1.next != null && l2.next != null){ 
            l1 = l1.next;
            l2 = l2.next;
            int sum = l1.val + l2.val + carry;
            if (sum >= 10){
                carry = sum/10;
                nd.next = new ListNode(sum%10);
                nd = nd.next;
            }
            else {
                nd.next = new ListNode(sum);
                nd = nd.next;
                carry = 0;
            }
        }

        if (l1.next == null && l2.next != null){
            // adding the last digit of l1 to the corresponding l2 digit
            int sum = l1.val + l2.val + carry;
            if (sum >= 10){
                carry = sum/10;
                nd.next = new ListNode(sum%10);
                nd = nd.next;
            }
            else {
                nd.next = new ListNode(sum);
                nd = nd.next;
            }
            l2 = l2.next;
            while (l2.next != null){
                int sum1 = l2.val + carry;
                if (sum1 >= 10){
                    carry = sum1/10;
                    nd.next = new ListNode(sum1%10);
                    nd = nd.next;
                } else {
                    carry = 0;
                    nd.next = new ListNode(sum1);
                    nd = nd.next;
                }
                l2 = l2.next;
            }
            int neww = l2.val + carry;
            if (neww >= 10){ // adding last digit to total sum
                carry = neww/10;
                nd.next = new ListNode(neww%10);
                nd = nd.next;
            } else {
                carry = 0;
                nd.next = new ListNode(neww);
                nd = nd.next;
            }
        }
        else if (l2.next == null && l1.next != null) {
            int sum = l1.val + l2.val + carry;
            if (sum >= 10){
                carry = sum/10;
                nd.next = new ListNode(sum%10);
                nd = nd.next;
            }
            else {
                nd.next = new ListNode(sum);
                nd = nd.next;
                carry = 0;
            }
            l1 = l1.next;
            while (l1.next != null){
                int sum2 = l1.val + carry;
                if (sum2 >= 10){
                    carry = sum2/10;
                    nd.next = new ListNode(sum2%10);
                    nd = nd.next;
                } else {
                    carry = 0;
                    nd.next = new ListNode(sum2);
                    nd = nd.next;
                }
                l1 = l1.next;
            }
            int neww = l1.val + carry;
            if (neww >= 10){
                carry = neww/10;
                nd.next = new ListNode(neww%10);
                nd = nd.next;
            } else {
                carry = 0;
                nd.next = new ListNode(neww);
                nd = nd.next;
            }
        }
        else { // both lists have same size
            int sum = l1.val + l2.val + carry;
            if (sum >= 10){
                carry = sum/10;
                nd.next = new ListNode(sum%10);
                nd = nd.next;
            }
            else {
                nd.next = new ListNode(sum);
                nd = nd.next;
                carry = 0;
            }
        }
        return lst.next;
    }
}

尝试这个:

public List<Integer> solution(List<Integer> a, List<Integer> b) {
    if (a.length != b.length)
        throw new AssertionError();

    List<Integer> result = new LinkedList<>(a.length);
    for (int i = 0; i < a.length; i++) {
        result.add(a.get(i) + b.get(i));
    } 
    return result;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM