Skip to content

Bug Report for three-integer-sum (will not update code after error) #5996

Description

@mosko333

Bug Report for https://neetcode.io/problems/three-integer-sum

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

I gave me a compiler error to turn "nums" into a let and not a var, but when I changed it and ran the code, it didn't detect the change, I tried reloading the page, hitting submit and run, reloading the problem, running on an empty answer, so far nothing works and it's still telling me to change nums to a let. here is the code:

class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
// [-1,0,1,2,-1,-4]
let nums = nums.sorted()
// [-4,-1,-1,0,1,2]
var results: [[Int]] = []

    var i = 0

    while (i < nums.count - 2) {
        // i == 0
        let target = -nums[i]
        // target == 4
        var j = i + 1
        // j == 1
        var k = nums.count - 1
        // k == 5

        while j < k {
            // [-4,-1,-1,0,1,2]
            let sum = nums[j] + nums[k]
            // sum == 0 + 2 == 2
            if target == sum {
                results.append([nums[i], nums[j], nums[k]])
                j += 1
                k -= 1
            }

            if sum < target {
                j += 1
                // j == 2
                while j < k && nums[j] == nums[j - 1] {
                    j += 1
                    // j == 3
                }
            } else if sum > target {
                k -= 1
            }
            while j < k && nums[j] == nums[j - 1] {
                j += 1
            }
            while j < k && nums[k] == nums[k + 1] {
                k -= 1
            }
        }
        i += 1
        while (i < nums.count - 2 && nums[i] == nums[i - 1]) {
            i += 1
        }
    }
    
    return results
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions