Skip to content

Commit aa0aba8

Browse files
authored
Update 0014_Problem_1.rb
1 parent 07f7424 commit aa0aba8

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Diff for: 0014_Problem_1.rb

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Problem Link (Contest): https://leetcode.com/contest/leetcode-weekly-contest-14/problems/number-complement/
33
# Problem Link (Practice): https://leetcode.com/problems/number-complement/
44

5+
# Solution 1
56
def find_complement(num)
67
return num.to_s(2)
78
.chars
@@ -11,3 +12,12 @@ def find_complement(num)
1112
.map { |val, i| val * ( 2 ** i) }
1213
.sum
1314
end
15+
16+
# Solution 2 (Better)
17+
def find_complement(num)
18+
return num.to_s(2)
19+
.chars
20+
.map { |x| 1 - x.to_i }
21+
.join
22+
.to_i(2)
23+
end

0 commit comments

Comments
 (0)