Leetcode No.78&90 Subsets

There are to algorithm problem about backtracking.

78. Subsets

Description

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

Answer

My answer is using recursion with a helper function, which is easy to understand.

90. Subsets II

Description

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

Answer

What the difference from the first one, is that the array contains duplicates, which we should ignore in our answer.
As a result, using a sort function is necessary and we have to judge in the helper if the number is duplicate.

Leave a Reply

Your email address will not be published. Required fields are marked *