【Help】Why my javascript code not work?


  • 0

    Here is my code:

    /**
     * @param {number[]} nums
     * @param {number} k
     * @return {number}
     */
    var findPairs = function(nums, k) {
        nums = nums.sort()
        var cnt = 0, tmp = nums[0] - 1
        if (k == 0) {
            for (var i = 0; i < nums.length - 1; tmp = nums[i], i++) {
                if (nums[i] == tmp) {
                    continue;
                }
                if (nums[i + 1] == nums[i]) {
                    cnt++;
                }
            }
        }
        if (k > 0) {
            for (i = 0, tmp; i < nums.length - 1; tmp = nums[i], i++) {
                if (tmp == nums[i]) {
                    continue;
                }
                for (var j = 0; j < nums.length; j++) {
                    if (nums[j] - nums[i] < k) {
                        continue;
                    }
                    if (nums[j] - nums[i] == k) {
                        cnt++;
                        break;
                    }
                    if (nums[j] - nums[i] > k) {
                        break;
                    }
                }
            }
        }
        return cnt
    };
    

    I have tried this method on C's and java, it all works. However, when it comes to js, it fails:

     Input:     [-1,-2,-3]
                1
    Output:     1
    Expected:   2 
    

    I am new to js, pls help me.


  • 1
    Y
    This post is deleted!

  • 0

    @yixuanwang.start I see. Thank you very much


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.