youngyangyang04 / leetcode-master-comment

用来做评论区
0 stars 0 forks source link

[Vssue]回溯算法去重问题的另一种写法.md #100

Open youngyangyang04 opened 6 months ago

youngyangyang04 commented 6 months ago

https://www.programmercarl.com/%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%E5%8E%BB%E9%87%8D%E9%97%AE%E9%A2%98%E7%9A%84%E5%8F%A6%E4%B8%80%E7%A7%8D%E5%86%99%E6%B3%95.html

MaTeng-Tech commented 4 months ago

对于排序后的数组,直接使用int preVal记录同一层节点的上一个值也可以吧,判断同一层元素重复出现只需要nums[i] == preVal 或者nums[i] == nums[i-1]

        int preVal = INT_MAX;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == preVal) {
                continue;
            }
            if (find(curSetIdx.begin(), curSetIdx.end(), i) != curSetIdx.end()) {
                continue;
            }
            preVal = nums[i];
            curSet.push_back(nums[i]);
            curSetIdx.push_back(i);
            backtraking(nums);
            curSet.pop_back();
            curSetIdx.pop_back();
        }
zhengzhengxing commented 2 months ago

47.全排列II,同层用set去重的话可以不用排序