Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

javascript - 将两个数字相加(Add Two Numbers Algo Understanding)

I was doing following leetcode question:

(我在做以下leetcode问题:)

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.

(您可能会假设两个数字除了数字0本身以外都不包含任何前导零。)

For which I stumbled upon following algo

(为此我偶然发现了算法)

function ListNode(val) {
      this.val = val;
      this.next = null;
 }

 //This functiomn is called
var addTwoNumbers = function(l1, l2) {    
    let remainder = 0
    let l3 = {}
    let head = l3
    while (l1 || l2 || remainder) {
        let sum = remainder
        function sumList (linkedList) {
            if (linkedList) {
                sum += linkedList.val
                return linkedList.next
            }
            return null
        }
        l1 = sumList(l1)
        l2 = sumList(l2)
        if (sum>9) {
            remainder = 1
            sum -= 10
        } else {
            remainder = 0
        }

        head.next = new ListNode(sum)
        head = head.next
    }
    return l3.next
};

Here, I am unable to comprehend the point of l3?

(在这里,我无法理解l3的观点?)

If i remove it the algo fails?

(如果我将其删除,算法会失败吗?)

Question Link: https://leetcode.com/problems/add-two-numbers/

(问题链接: https : //leetcode.com/problems/add-two-numbers/)

  ask by anny123 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

l3 is there to keep a record of all the values that have been changed for head.

(l3在那里保留所有已更改为head的值的记录。)

If I say that:

(如果我这样说:)

var a = {};
var b = a;

Then b is not its own object.

(那么b不是它自己的对象。)

It's a pointer to the same exact object as a, so if you change a value in b, a updates to reflect that change as well.

(它是与a相同的对象的指针,因此,如果您更改b中的值,则也会进行更新以反映该更改。)

That is still the case when you dive deeper into nested objects.

(当您深入嵌套对象时,情况仍然如此。)

If I say that:

(如果我这样说:)

var c = {value: 1, next:{innerValue: 1}};
var d = c;
d.value = 2;

Then c updates to reflect the new value as well, as I've already explained.

(正如我已经解释的,然后c也会更新以反映新值。)

But the interesting part is that if I then go on to assign d to the nested object and edit the nested object, like so:

(但是有趣的是,如果我继续将d分配给嵌套对象并编辑嵌套对象,如下所示:)

d = d.next;
d.innerValue = 2;

That still updates c!

(仍然更新c!)

So c is keeping a record of all of these updates, while d is free to dive deeper into its nested objects.

(因此,c保留所有这些更新的记录,而d可以自由地更深入地研究其嵌套对象。)

After running this code, c and d will look like this:

(运行此代码后,c和d将如下所示:)

console.log(c);//{value: 2, next:{innerValue: 2}}
console.log(d);//{innerValue: 2}

This is what l3 does.

(这就是l3所做的。)

l3 is to head as c is to d, so when the algorithm says:

(l3朝向c朝向d,所以当算法说:)

head.next = new ListNode(sum)
head = head.next

...you don't have to worry about losing your history because l3 is keeping a record of everything.

(...您不必担心会丢失历史记录,因为l3会保留所有记录。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...