Counting through all binary numbers with equal 1's and 0's
问题 I'm implementing a binary representation of an equal-side bi-partitioning algorithm and I'm wondering what the best way to iterate through all combinations of N bits that have equal (N/2) 1's and 0's. I'm trying to find the quickest way, not the easiest to code. Thanks. 回答1: It's just (N choose N/2) ; you're choosing which bits are 0s, the rest are 1s. If you have 10 bits, and you want 5 zeroes and 5 ones, there are (10 choose 5) = 252 possibilities. See also: Algorithm to return all