How do i generate all possible subsets of an binary string?

后端 未结 5 1947
隐瞒了意图╮
隐瞒了意图╮ 2021-01-20 22:06

i have a problem, that i don\'t know how to solve it.

i have a binary string and i want to generate all possible binary substrings.

Example :



        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 22:49

    Magic - assumes bitmask though:

    subset( int x ) 
        list = ()
        for ( int i = x; i >= 0; i = ( ( i - 1 ) & x ) )
              list.append( i )
        return list
    

    You can use the same logic, though it's a little bit more involved, with a binary string.

提交回复
热议问题