-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0371_sum_of_two_integers.rs
More file actions
66 lines (59 loc) · 1.52 KB
/
s0371_sum_of_two_integers.rs
File metadata and controls
66 lines (59 loc) · 1.52 KB
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
63
64
65
66
#![allow(unused)]
pub struct Solution {}
impl Solution {
// string O(1) O(1)
pub fn get_sum_string(a: i32, b: i32) -> i32 {
let (mut x, mut y) = (i32::abs(a), i32::abs(b));
// ensure that abs(a) >= abs(b)
if x < y {
return Self::get_sum(b, a);
}
// abs(A) >= abs(b) --> a determines the sign
let mut sign = 1;
if a < 0 {
sign = -1;
}
if a * b >= 0 {
// sum of two positive integers x + y
// where x > y
let (mut answer, mut carry) = (0, 0);
while y != 0 {
answer = x ^ y;
carry = (x & y) << 1;
x = answer;
y = carry;
}
} else {
// difference of two integers x - y
// where x > y
let (mut answer, mut borrow) = (0, 0);
while y != 0 {
answer = x ^ y;
borrow = ((!x) & y) << 1;
x = answer;
y = borrow;
}
}
x * sign
}
// O(1) O(1)
pub fn get_sum(mut a: i32, mut b: i32) -> i32 {
let (mut answer, mut carry) = (0, 0);
while b != 0 {
answer = a ^ b;
carry = (a & b) << 1;
a = answer;
b = carry;
}
a
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_371() {
assert_eq!(Solution::get_sum(1, 2), 3);
assert_eq!(Solution::get_sum(-2, 3), 1);
}
}