-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
47 lines (40 loc) · 851 Bytes
/
lib.rs
File metadata and controls
47 lines (40 loc) · 851 Bytes
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
/*!
* # 7. Reverse Integer
*
* * [Problem link](https://leetcode.com/problems/reverse-integer/)
*/
#![allow(dead_code)]
struct Solution {}
impl Solution {
pub fn reverse(x: i32) -> i32 {
let positive_reversed = x
.abs()
.to_string()
.chars()
.rev()
.collect::<String>()
.parse()
.unwrap_or(0);
if x.is_positive() {
positive_reversed
} else {
-positive_reversed
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example_1() {
assert_eq!(Solution::reverse(123), 321);
}
#[test]
fn test_example_2() {
assert_eq!(Solution::reverse(-123), -321);
}
#[test]
fn test_example_3() {
assert_eq!(Solution::reverse(120), 21);
}
}