Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

排列组合问题 #17

Open
heyLiup opened this issue Jul 17, 2022 · 0 comments
Open

排列组合问题 #17

heyLiup opened this issue Jul 17, 2022 · 0 comments

Comments

@heyLiup
Copy link
Owner

heyLiup commented Jul 17, 2022

1、给一个升序的有重复元素的数组,返回不重复的子集

function dfs (arr, index) {
        if (arr.length <= nums.length) {
            res.push(arr.slice());
        }
        for (let i = index; i < nums.length; i++) {
            if (i === index || nums[i] !== nums[i - 1] ) {  // 判断当前元素与上一个元素不同
                arr.push(nums[i]);
                dfs(arr, i + 1);
                arr.pop();
            }
           
        }
    }

2、给一个没有重复元素的数组,返回不重复的子集

 function dfs (arr, index) {
        if (arr.length <= nums.length) {
            res.push(arr.slice());
        }
        for (let i = index; i < nums.length; i++) {
            arr.push(nums[i]);
            dfs(arr, i + 1);  // 使用 i + 1
            arr.pop();
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant