-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_integer.rs
59 lines (54 loc) · 1.26 KB
/
reverse_integer.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
/**
* m5: overflow-aware integer reverse
*
* The solution reverses an integer that is overflow-aware.
*
* If the integer overflows while reversing its digits, it will return 0
*
*
* To run or test codes, run the following:
* =============================================================================
*
* cargo run --bin m5
* cargo test --bin m5
*
* =============================================================================
*/
struct Solution;
impl Solution {
pub fn reverse(mut x: i32) -> i32 {
let mut result: i32 = 0;
loop {
result = match result.checked_mul(10).and_then(|n| n.checked_add(x % 10)) {
Some(res) => res,
None => {
result = 0;
break;
}
};
x /= 10;
if x == 0 {
break;
};
}
result
}
}
fn main() {
println!(
"reverse of 2147483647 is: {}",
Solution::reverse(2147483647)
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test_reverse() {
assert_eq!(Solution::reverse(1024), 4201);
}
#[test]
fn test_reverse_overflow() {
assert_eq!(Solution::reverse(2147483647), 0);
}
}