for example, given a number 210 (11010010 in binary), where bits 2, 5, 7, and 8 are 1 (starting from the rightmost), then the result is [2 mine5, 7, 8].
are there any efficient solutions?
for example, given a number 210 (11010010 in binary), where bits 2, 5, 7, and 8 are 1 (starting from the rightmost), then the result is [2 mine5, 7, 8].
are there any efficient solutions?
the level is limited.
I don't know what kind of efficient solution you want, but I tried it myself. In python, what I think should be efficient is actually not efficient. Instead, I think the stupidest method is more efficient.
public static List<Integer> getBitCollections (long bits) {
List<Integer> index = new ArrayList<>();
int position = 1;
while (bits != 0) {
if ((bits & 1) != 0) {
index.add(position);
}
positionPP;
bits = bits >>> 1;
}
return index;
}
Previous: Express runtime error