Last active
July 4, 2019 17:46
-
-
Save manojnaidu619/079e231d67c4a3d893b4e2209e8eb89e to your computer and use it in GitHub Desktop.
Leetcode Solution for "Number Complement" problem in Ruby
This file contains hidden or 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
=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