Skip to content

Commit 758edcc

Browse files
committed
add binary numbers
1 parent 4a05883 commit 758edcc

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This is my way of saying - change your interview style. There are lots of smart
2929
| [Dijkstra's shortest path between two nodes](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/dijkstra.rb) |
3030
| [Kruskal's minimum spanning tree algorithm](http://en.wikipedia.org/wiki/Kruskal%27s_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/kruskal.rb) |
3131
| [Find the square root of a number](https://en.wikipedia.org/wiki/Newton%27s_method) | [click](https://github.com/sagivo/algorithms/blob/master/src/sq_root.rb) |
32+
| [Add two binary numbers](https://leetcode.com/problems/add-binary/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/add_binary.rb) |
3233
| [Binary search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/binary_search.rb) |
3334
| [Longest increasing subsequence](http://en.wikipedia.org/wiki/Longest_increasing_subsequence) | [click](https://github.com/sagivo/algorithms/blob/master/src/longest_increasing_subsequence.rb) |
3435
| [Find all permutations of array](https://en.wikipedia.org/wiki/Permutation) | [click](https://github.com/sagivo/algorithms/blob/master/src/permutations.rb) |

src/add_binary.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# problem: https://leetcode.com/problems/add-binary
2+
# @param {String} a
3+
# @param {String} b
4+
# @return {String}
5+
def add_binary(a, b)
6+
i = 0
7+
cerry = 0
8+
res = ''
9+
10+
while (i < a.size) || (i < b.size) do
11+
a1 = i<a.size ? a[a.size-1 - i].to_i : 0
12+
b1 = i<b.size ? b[b.size-1 - i].to_i : 0
13+
num = a1 ^ b1 ^ cerry
14+
res += num.to_s
15+
cerry = (a1 + b1 + cerry) >= 2 ? 1 : 0
16+
i+=1
17+
end
18+
res += '1' if cerry == 1
19+
20+
return res.reverse
21+
end
22+
23+
p add_binary('1010','1011') #=> '10101'

src/phone.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
def letters_for_number num
1+
def letters_for_number num
22
case num
33
when 0, 1
44
[num]
5-
when 2
6-
['A', 'B', 'C']
7-
when 3
5+
when 2
6+
['A', 'B', 'C']
7+
when 3
88
['D', 'E', 'F']
9-
when 4
9+
when 4
1010
['G', 'H', 'I']
11-
when 5
11+
when 5
1212
['J', 'K', 'L']
13-
when 6
13+
when 6
1414
['M', 'N', 'O']
15-
when 7
15+
when 7
1616
['P', 'Q', 'R', 'S']
17-
when 8
17+
when 8
1818
['T', 'U', 'V']
19-
when 9
19+
when 9
2020
['W' ,'X', 'Y', 'Z']
2121
else
2222
[]
23-
end
23+
end
2424
end
2525

2626
def join_sets set, i = 0, str = '', result = []
2727
return result << str if str.size == set.size
28-
for item in set[i]
28+
for item in set[i]
2929
join_sets set, i.succ, str + item.to_s, result
3030
end
3131
result

0 commit comments

Comments
 (0)