List of all binary combinations for a number in Java

后端 未结 6 1057
后悔当初
后悔当初 2021-01-14 05:15

I am working on a project involving \"Dynamic Programming\" and am struck on this trivial thing, please help.

Suppose I take 4 as an input, I want to display somethi

6条回答
  •  攒了一身酷
    2021-01-14 06:10

    Either use phoxis's very nice solution, or just iterate them lexicographically (this is really the same solution!): Given a binary string of a given length, get the next lexicographic string by finding the rightmost zero entry, change it to a 1, and change everything to the right of it back to a 0, e.g.

    0 0 0
    0 0 1
    0 1 0
    0 1 1
    1 0 0
    1 0 1
    1 1 0
    1 1 1
    

提交回复
热议问题