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
62
pub fn run(mut a: Vec<u8>, mut b: Vec<u8>) -> Vec<u8> {
let mut res: Vec<u8> = Vec::new();
while a.len() > 0 || b.len() > 0 {
let a_bit = match a.pop() {
Some(r) => r,
None => 0
};
let b_bit = match b.pop() {
Some(r) => r,
None => 0
};
if b_bit > a_bit {
let mut borrowed: bool = false;
let mut borrow_index = a.len() - 1;
while !borrowed {
if a[borrow_index] == 1 {
a[borrow_index] = 0;
borrowed = true;
} else {
a[borrow_index] = 1;
borrow_index -= 1;
}
}
res.push(1);
} else {
let diff = a_bit - b_bit;
res.push(diff);
}
}
res.reverse();
while res.len() > 1 && res[0] == 0 {
res.remove(0);
}
res
}