简体   繁体   English

Leetcode 3 求和不同的答案 python vs javascript

[英]Leetcode 3 sum different answer python vs javascript

The 3 sum problem is a well known coding interview problem. 3 和问题是一个众所周知的编码面试问题。 It states the following:它声明如下:

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i,= j, i,= k.给定一个 integer 数组 nums,返回所有满足 i,= j, i,= k 的三元组 [nums[i], nums[j], nums[k]]。 and j != k, and nums[i] + nums[j] + nums[k] == 0.并且 j != k,以及 nums[i] + nums[j] + nums[k] == 0。

I understand the algorithm and have coded the solution in python, but I recently tried to translate the python code below into javascript and I'm getting an incorrect answer.我了解该算法并将解决方案编码为 python,但我最近尝试将下面的 python 代码翻译成 javascript,但我得到的答案不正确。

This is very strange to me, because the code is completely translated , line-by-line.这对我来说很奇怪,因为代码是完全翻译的,一行一行的。 Unless javascript's recursive runtime stack under-the-hood has a different implementation of python (unlikely).除非 javascript 的递归运行时堆栈在后台具有 python 的不同实现(不太可能)。 Is there something more to why the code gives different results on [-1,0,1,2,-1,-4] ?为什么代码在[-1,0,1,2,-1,-4]上给出不同的结果还有更多原因吗?

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        def kSum(nums: List[int], target: int, k: int, startIndex: int) -> List[List[int]]:
            if k == 2:
                ts = twoSum(nums, target, startIndex)
                return ts
            
            res = []
            for i in range(startIndex, len(nums)):
                currNum = nums[i]
                takeCurr = target - currNum
                if i == 0 or nums[i - 1] != nums[i]:
                    found = kSum(nums, target=takeCurr, k=k-1, startIndex=i+1)
                    for subset in found:
                        temp = [currNum] + subset
                        res.append(temp)
            return res

        def twoSum(nums: List[int], target: int, startIndex: int) -> List[List[int]]:
            res = []
            lo = startIndex
            hi = len(nums)-1
    
            while (lo < hi):
                curr_sum = nums[lo] + nums[hi]
                if curr_sum < target:
                    lo += 1
                elif curr_sum > target:
                    hi -= 1
                else:
                    res.append([nums[lo], nums[hi]])
                    lo += 1
                    while (lo < len(nums) and nums[lo] == nums[lo-1]):
                        lo+=1

            return res

        nums.sort()
        return kSum(nums, target=0, k=3, startIndex=0)
function threeSum (nums) {
    function kSum(nums, target, k, startIndex) {
        if (k===2) {
            let ts = twoSum(nums, target, startIndex);
            return ts;
        }

        let res = [];
        for (let i = startIndex; i < nums.length; ++i) {
            const currNum = nums[i];
            const takeCurr = target - currNum;
            if (i === 0 || nums[i-1] != nums[i]) {
                let found = kSum(nums, target=takeCurr, k=k-1, startIndex=i+1);
                for (const subset of found) {
                    let temp = [currNum].concat(subset);
                    res.push(temp);
                }
            }
        }
        return res;
    }

    function twoSum(nums, target, startIndex) {
        let res = [];
        let lo = startIndex;
        let hi = nums.length-1;
        while (lo < hi) {
            const curr_sum = nums[lo] + nums[hi];
            if (curr_sum < target) {
                lo++;
            }
            else if (curr_sum > target) {
                hi--;
            }
            else {
                res.push([nums[lo], nums[hi]]);
                lo++;

                while (lo < nums.length && nums[lo] === nums[lo-1]) {
                    lo++;
                }
            }
        }
        return res;
    }
    
    nums.sort(function(a,b) { return a-b;});
    return kSum(nums, target=0, k=3, startIndex=0);
}






JavaScript does not support named arguments. This causes your code to work in unexpected ways and therefore should be removed. JavaScript不支持命名为 arguments。这会导致您的代码以意想不到的方式工作,因此应该删除。

Because you use the same variable name in the nested scope's args, you're overwriting the value of the variable in the outer scope when using named arguments.因为您在嵌套作用域的 args 中使用相同的变量名称,所以当使用命名 arguments 时,您将覆盖外部 scope 中的变量值。

Simple example:简单示例:

let a = 1;
foo(a=2);
console.log(a); // 2

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

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