【问题标题】:Adding two numbers that are represented as graphs添加两个以图形表示的数字
【发布时间】:2016-05-24 15:59:15
【问题描述】:

我正在尝试解决 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]

我添加了 cmets 以提高可读性。

/**
* 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;
    }
}

【问题讨论】:

  • 向我们展示 addTwoNumbers() 是如何与链表值一起调用的
  • 不知道方法是怎么调用的,网站自己调用的。它只是为我们提供了 ListNode 类的结构,它位于第一个注释块中。

标签: java algorithm linked-list nodes


【解决方案1】:

试试这个:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2012-01-18
    • 2020-04-07
    相关资源
    最近更新 更多