Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active July 4, 2019 17:46
Show Gist options
  • Save manojnaidu619/079e231d67c4a3d893b4e2209e8eb89e to your computer and use it in GitHub Desktop.
Save manojnaidu619/079e231d67c4a3d893b4e2209e8eb89e to your computer and use it in GitHub Desktop.
Leetcode Solution for "Number Complement" problem in Ruby
=begin
687.to_s(2) # "1010101111"
"1010101111".to_i(2) # 687
=end
def find_complement(num)
num = num.to_s(2).chars
num.map!{|i| i.to_i}
for x in 0..num.size-1 do
if num[x]==0
num[x] = 1
else
num[x] = 0
end
end
p num.join.to_i(2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment