Created
April 4, 2018 12:02
-
-
Save heat/484b4a965b61ebd0eb24eb4c94db07ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getOneBits(n: Int): Array[Int] = { | |
var start: Int = 0 | |
var total: Int = 0 | |
val bin = n.toBinaryString | |
bin.foreach( c => if(c == '1') total += 1) | |
var positions = List(total) | |
for ((c, i) <- bin.zip(Stream from 1)) { | |
if(c == '1' && start == 0) start = i | |
if(c == '1') { | |
positions = i :: positions | |
} | |
} | |
positions = positions.reverse | |
return positions.toArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment