Can we have a solution using bit manipulation for Subsets II?

  • 0

    For Subsets I we already have one:

    I'm wondering how to come up with a similar solution for Subsets II. Can anyone help?

  • 0

    We can also declare two paras indicating the last value and the bit value of last value.

    For example:
    lastvalue and lastbitvalue

    lst = [1,2,2,3]

    so "1101" is valid,
    "1011" is not valid because it is the same as "1101"

    the additional judgement comparing with Subsets I is :(in Python)

    if S[j] == lastvalue and lastbitvalue == 0 and i & (1<<j):
           #clear the list to be appended to return list which is constructed in the previous loop
           #break and return to the loop of i

    If you use bit manipulation in Subsets I, you can understand i and j easily.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.