-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_binary_67.rs
executable file
·61 lines (60 loc) · 1.74 KB
/
add_binary_67.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::Solution;
impl Solution {
pub fn add_binary(mut a: String, mut b: String) -> String {
let mut result = String::new();
let mut carry: u8 = 0;
while a.len() < b.len() {
a.insert(0, '0');
}
while b.len() < a.len() {
b.insert(0, '0');
}
let a = a.chars().collect::<Vec<_>>();
let b = b.chars().collect::<Vec<_>>();
for i in (0..a.len()).rev() {
let mut bitstring: String = String::new();
bitstring.push(a[i]);
bitstring.push(b[i]);
bitstring.push(if carry == 1 { '1' } else { '0' });
match &bitstring[..] {
"000" => {
result.push('0');
carry = 0;
}
"001" => {
result.push('1');
carry = 0;
}
"010" => {
result.push('1');
carry = 0;
}
"011" => {
result.push('0');
carry = 1;
}
"100" => {
result.push('1');
carry = 0;
}
"101" => {
result.push('0');
carry = 1;
}
"110" => {
result.push('0');
carry = 1;
}
"111" => {
result.push('1');
carry = 1;
}
_ => panic!("This won't happen"),
}
}
if carry == 1 {
result.push('1');
}
return result.chars().rev().collect();
}
}