Skip to content

Commit 6861aeb

Browse files
author
Partho Biswas
committed
67_Add_Binary
1 parent 9909cd9 commit 6861aeb

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
730730

731731
### 17. Bit Manipulation
732732

733+
**Follow [this](https://tinyurl.com/vmmaxbe) golden rule to approach any DP problem.**
734+
733735
<details><summary>Leetcode problems with solutions and tutorials/videos</summary>
734736
<p>
735737

@@ -749,6 +751,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
749751
|12| [260. Single Number III](https://tinyurl.com/y4hgvu2y)| [Python](https://tinyurl.com/wu6rdaw/260_Single_Number_III.py)| **[Educative.io](https://tinyurl.com/tdhj9eh)** | Medium | ⭐ Check again, very important |
750752
|13| [476. Number Complement](https://tinyurl.com/tadg5uf)| [Python](https://tinyurl.com/wu6rdaw/476_Number_Complement.py)| **[Educative.io](https://tinyurl.com/ucwmq5n)** | Easy | ⭐ Check again |
751753
|14| [832. Flipping an Image](https://tinyurl.com/uog7h9f)| [Python](https://tinyurl.com/wu6rdaw/832_Flipping_an_Image.py)| **[Educative.io](https://tinyurl.com/wyd8c4g)** | Easy | ⭐ Check again |
754+
|15| [67. Add Binary](https://tinyurl.com/y6hbtal7)| [Python](https://tinyurl.com/wu6rdaw/67_Add_Binary.py), [Swift](https://tinyurl.com/wuja3c4/67_Add_Binary.swift) | **[Official](https://tinyurl.com/y7w7ry8e)**, [Art 1](https://tinyurl.com/yaxg48ag), [Art 2](https://tinyurl.com/y77e2ylc) | Easy | ⭐ Check again |
752755

753756
</p>
754757
</details>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
func addBinary(_ a: String, _ b: String) -> String {
3+
guard var x = Int(a, radix: 2), var y = Int(b, radix: 2) else {
4+
print("x: \(Int(a, radix: 2)), y: \(Int(b, radix: 2))")
5+
return "0"
6+
}
7+
print("x: \(x), y: \(y)")
8+
while y > 0 {
9+
let answer = x ^ y
10+
let carry = (x & y) << 1
11+
(x, y) = (answer, carry)
12+
}
13+
let answer = String(x, radix: 2)
14+
print(answer)
15+
print("x: \(x), y: \(y)")
16+
return answer
17+
}
18+
}
19+
/*
20+
Input:
21+
a = "11" >> 3
22+
b = "1" >> 1
23+
24+
Output: "100" >> 4
25+
26+
27+
28+
Input:
29+
a = "1010" >> 10
30+
b = "1011" >> 11
31+
32+
Output: "10101" >> 21
33+
*/

0 commit comments

Comments
 (0)