-
Notifications
You must be signed in to change notification settings - Fork 2
/
add-binary.rs
61 lines (51 loc) · 1.59 KB
/
add-binary.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
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
impl Solution {
pub fn add_binary(a: String, b: String) -> String {
if a == b && a == "0".to_string() {
return a;
}
let (a, b) = (a.as_bytes(), b.as_bytes());
let mut s = vec![b' '; a.len().max(b.len()) + 1];
let (mut m, mut index) = ((b'0', b'0'), 0);
while a.len() >= index || b.len() >= index {
let a1 = if a.len() >= index {
a[a.len() - index - 1]
} else {
b'0'
};
let b1 = if b.len() >= index {
b[b.len() - index - 1]
} else {
b'0'
};
m = Self::add(a1, b1, m.1);
let l = s.len() - index - 1;
s[l] = m.0;
index += 1;
}
if m.1 == b'1' {
let l = s.len() - index - 1;
s[l] = b'1';
}
String::from_utf8(s)
.unwrap()
.trim_start_matches('0')
.to_string()
}
/// 返回a+b的和与进制
fn add(a: u8, b: u8, c: u8) -> (u8, u8) {
match (a, b, c) {
(b'1', b'1', b'1') => (b'1', b'1'),
(b'0', b'1', b'1') => (b'0', b'1'),
(b'1', b'0', b'1') => (b'0', b'1'),
(b'1', b'1', b'0') => (b'0', b'1'),
(b'0', b'0', b'1') => (b'1', b'0'),
(b'0', b'1', b'0') => (b'1', b'0'),
(b'1', b'0', b'0') => (b'1', b'0'),
(b'0', b'0', b'0') => (b'0', b'0'),
_ => (b'0', b'0'),
}
}
}