S2 icon indicating copy to clipboard operation
S2 copied to clipboard

0002. Add Two Numbers | LeetCode Cookbook

Open halfrost opened this issue 5 years ago • 7 comments

https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0002.Add-Two-Numbers/

halfrost avatar Aug 14 '20 03:08 halfrost

大神, 我有一个类似的解法,为什么提交的时候系统判定 runtime error: out of memory func addTwoNumbers_solution1(l1 *ListNode, l2 *ListNode) *ListNode { var head *ListNode var current *ListNode var previous *ListNode

n1, n2, carry := 0, 0, 0

for i := 0; l1 != nil || l2 != nil || carry != 0; i++ {
	if l1 == nil {
		n1 = 0
	} else {
		n1 = l1.Val
		l1 = l1.Next
	}
	if l2 == nil {
		n2 = 0
	} else {
		n2 = l2.Val
		l2 = l2.Next
	}

	current = &ListNode{Val: (n1 + n2 + carry) % 10}
	if i == 0 {
		head = current
		previous = current
	}
	previous.Next = current
	previous = current
	carry = (n1 + n2 + carry) / 10
}

return head

}

xqianwang avatar Sep 02 '20 03:09 xqianwang

@xqianwang 大神, 我有一个类似的解法,为什么提交的时候系统判定 runtime error: out of memory func addTwoNumbers_solution1(l1 *ListNode, l2 *ListNode) *ListNode { var head *ListNode var current *ListNode var previous *ListNode

n1, n2, carry := 0, 0, 0

for i := 0; l1 != nil || l2 != nil || carry != 0; i++ { if l1 == nil { n1 = 0 } else { n1 = l1.Val l1 = l1.Next } if l2 == nil { n2 = 0 } else { n2 = l2.Val l2 = l2.Next }

  current = &ListNode{Val: (n1 + n2 + carry) % 10}
  if i == 0 {
  	head = current
  	previous = current
  }
  previous.Next = current
  previous = current
  carry = (n1 + n2 + carry) / 10

}

return head

}

您好,你这个代码的问题在于链表成环了,所以最终导致内存爆掉了。你可以试试这样的测试用例:[]int{1}, []int{1},两个链表都只有一个数字,你就能发现你 previous.Next = current 这句话就会导致链表成环了。

halfrost avatar Sep 03 '20 02:09 halfrost

有个类似letcode的检测工具就好了 确保自己的方案测试覆盖率和beat100啥的

hujun2020 avatar Jun 02 '21 01:06 hujun2020

@hujun2020 测试覆盖率有现成的工具,每次写完自己手动本地跑一下就行。beats 100% 这个工具实现不了,因为是 LeetCode 后端检测 runtime ,然后排名的,并且排名也是动态的。随时会有人超过你。

halfrost avatar Jun 02 '21 03:06 halfrost

python打卡

class ListNode:
    def __init__(nacho, val = 0, next = None):
        nacho.val = val
        nacho.next = next
l1List = [ListNode(int(_)) for _ in input("Input: l1 = ").split()]
l2List = [ListNode(int(_)) for _ in input("l2 = ").split()]
for l in [l1List, l2List]:
    for index in range(len(l) - 1):
        l[index].next = l[index + 1]
l1, l2 = l1List[0], l2List[0]

answer = pa = ListNode((l1.val + l2.val) % 10)
carry = (l1.val + l2.val) // 10
l1, l2 = l1.next, l2.next
while l1 or l2:
    val = 0
    if l1:
        val += l1.val
        l1 = l1.next
    if l2:
        val += l2.val
        l2 = l2.next
    val += carry
    pa.next = ListNode(val % 10)
    pa = pa.next
    carry = val // 10
if carry:
    pa.next = ListNode(carry)

print("Output: ", end = '[')
print(answer.val, end = '')
answer = answer.next
while answer:
    print(', ', end = '')
    print(answer.val, end = '')
    answer = answer.next
print(']')

有点长,实际提交的是中间一段,前面是构造leetcode的数据形式,后面打印是方便自己调试

chiyoi avatar Aug 08 '21 10:08 chiyoi

Java打卡2. 两数相加

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode node = head;
        int carry = 0;

        while (carry > 0 || l1 != null || l2 != null) {
            int num = carry;
            if (l1 != null) {
                num += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                num += l2.val;
                l2 = l2.next;
            }
            carry = num / 10;
            num %= 10;

            node.next = new ListNode(num);
            node = node.next;
        }
        return head.next;
    }
}

chen-gongzhe avatar Dec 03 '21 12:12 chen-gongzhe

LinkedList<int> Add(LinkedList<int> a, LinkedList<int> b)
{
	var af = a.First;
	var bf = b.First;

	LinkedList<int> r = new(new[] {0});
	LinkedListNode<int> rf = r.First;
	int carry = 0;
	while (af != null || bf != null || carry != 0)
	{
		int value = (af?.Value ?? 0) + (bf?.Value ?? 0) + carry;
		carry = value / 10;
		r.AddAfter(rf, value % 10);
		rf = rf.Next;
		af = af?.Next;
		bf = bf?.Next;
	}
	r.RemoveFirst();
	return r;
}

C# 打卡

LGinC avatar Feb 09 '22 04:02 LGinC