LeetCode-Go icon indicating copy to clipboard operation
LeetCode-Go copied to clipboard

There is a bug in your Leetcode-go 39 combination sum

Open shiiaii opened this issue 1 year ago • 3 comments

You code does not skip the duplicated combination. You need to add skip duplicated combination . See below.

        if nums[i] > target  {
            break
        }
        // the following to skip the duplicated combination
        if (len(c)!=0 && nums[i] < c[len(c)-1]) {
            continue
        }

shiiaii avatar Aug 29 '24 02:08 shiiaii

呵呵,来信收到,谢谢

haowan1015 avatar Aug 29 '24 02:08 haowan1015

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

xiaokuer avatar Aug 29 '24 02:08 xiaokuer

@shiiaii Hi, about deduplication mechanism:

  • Since in each recursive call the index only moves forward (i starts from index), even if there are duplicate numbers in the array, they will not result in duplicate combinations.
  • For example, for the array [2, 3, 6, 7] and the target value 7, the combination [2, 2, 3] will only be considered once when the loop reaches 2. Subsequent iterations will not consider [2, 3, 2] or [3, 2, 2], thus avoiding duplicate combinations.

You can try submitting my code and you will get the result of accept. This is a screenshot of the result I just submitted.

image

halfrost avatar Sep 01 '24 09:09 halfrost